在布局 xml 中使用 onClick 属性会导致 Android 对话框中出现 NoSuchMethodException
我创建了一个自定义对话框和一个布局 xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tap Me"
android:onClick="dialogClicked" />
</LinearLayout>
在对话框类中,我实现了方法“dialogClicked(View v)”:
public class TestDialog extends Dialog {
public TestDialog(final Context context)
{
super(context);
}
@Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog);
}
public void dialogClicked(final View view)
{
System.out.println("clicked");
}
}
当我点击按钮时,我得到一个 NoSuchMethodException 'dialogClicked'。在布局 xml 中设置 onClick 处理程序适用于活动,但不适用于对话框。有什么想法吗?我做错了什么?
I have created a custom dialog and a layout xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tap Me"
android:onClick="dialogClicked" />
</LinearLayout>
In the dialog class I've implemented the method "dialogClicked(View v)":
public class TestDialog extends Dialog {
public TestDialog(final Context context)
{
super(context);
}
@Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog);
}
public void dialogClicked(final View view)
{
System.out.println("clicked");
}
}
When I tap the button I get a NoSuchMethodException 'dialogClicked'. Setting the onClick handler in layout xml works fine for activities, but not in dialogs. Any ideas? What I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
在Activity中定义方法(dialogClicked)。
并像下面的代码一样修改 TestDialog:
我认为它有效:)
Define the method (dialogClicked) in Activity.
And modify TestDialog like the following code:
I think it works :)
我认为这个问题是范围之一。我不确定你是如何在 xml 中解决这个问题的,但基本上布局 xml 中的DialogicClicked 方法不知道在哪里可以找到你在对话框类中定义的方法。
我见过的在自定义布局中绑定按钮的标准方法如下。
。
希望
有帮助。仍然有兴趣知道是否有使用 xml onClick 绑定到该方法的解决方案。也许 setContentView 中还有一个附加参数?其他的东西。
I think the issue is one of scope. I'm not sure how'd you address this in xml, but essentially the dialogueClicked method in your layout xml doesn't know where to find the method you've defined in the dialog class.
The standard approach i've seen to bind buttons in custom layouts is as follows.
.
}
Hope that helps. Would still be interested in knowing if there was a solution to using xml onClick to bind to the method. Perhaps an additional argument in the setContentView? something r'other.
我在 View.java 源代码中找到了以下代码:
->视图使用其上下文来解析 onclick 处理程序方法。
现在,以下代码来自 Dialog.java 源代码:
在对话框的构造函数中,创建 ContextThemeWrapper 的实例并将其设置为上下文。该实例既不是自定义对话框类,也不是调用活动,它可以是实现处理程序方法的地方。因此视图无法找到 onclick 处理程序方法。
但我必须使用 onclick XML 属性。有可用的解决方法吗?
I've found the following code in the View.java source:
-> The views uses its context to resolve the onclick handler method.
Noew the following code from Dialog.java source:
In the constructor of the dialog an instance of ContextThemeWrapper gets created and set as context. This instance is neither the custom dialog class, nor the calling activity, which can be the place for implementing the handler method. Therefore views are not able to find the onclick handler method.
But I have to use the onclick XML attribut. Any workarounds available?
对话框需要签名
Dialogs need the signature
android:onClick="method"
非常酷,但它在 Android 1.5 上不起作用,所以我回避了一段时间。一个简单的解决方法:
将您的
Dialog
设为Activity
并在您的android:theme="@android:style/Theme.Dialog"
中使用>AndroidManifest。android:onClick="method"
is pretty cool, but it doesn't work on Android 1.5 so I am avoiding for some time.An easy workaround:
Make your
Dialog
anActivity
and useandroid:theme="@android:style/Theme.Dialog"
in youAndroidManifest
.继 Jett Hsieh 的帖子之后,我使用 showDialog 和 DismissDialog 实现的对话框略有不同,但让 android:onClick 工作的基本原理是相同的,我的示例代码如下以供将来参考。
和布局文件:
Following on from Jett Hsieh's post, I've implemented my dialogs slightly differently using showDialog and dismissDialog, but the fundamentals of getting the android:onClick working have been the same, my example code is below for future reference.
And the layout file:
尝试在活动中而不是在对话框中定义该方法(dialogClicked)。
它可能使用反射,因此如果您使用不同的活动,只需在可能显示该对话框的每个活动中编写该方法
Try to define that method (dialogClicked) in the activity and not in the dialog.
It might use reflection so if you use different activities just write that method in each activity that might show that dialog
对话框总是作为活动的一部分创建和显示。根据Android References:
此外,您是否传递 getApplicationContext() 返回的对象?到 TestDialog 的构造函数?
A dialog is always created and displayed as part of an Activity. According to Android References:
Also, are you passing the object returned by getApplicationContext(); to the constructor of TestDialog?
系统在布局膨胀的位置或将 xml 设置为内容的活动类中查找方法。
system looks for the method in the where the layout has been inflated from, or in the activity class to which the xml was set as content.