我在操作栏上按下按钮时弹出一个对话框。在对话框中有一个带有几个按钮的布局。其中一个按钮我只是试图调用一个简单的方法来显示一个吐司,只是为了表明它正在工作。
这是显示对话框的初始按钮的方法:
public void optionsPressed(MenuItem item){
final Dialog optionsDialog = new Dialog(this);
optionsDialog.setTitle("Options");
optionsDialog.setContentView(R.layout.options);
optionsDialog.show();
}
这是布局的一部分,我想用来显示吐司的按钮:
<ImageButton
android:layout_width="@dimen/large_brush"
android:layout_height="@dimen/large_brush"
android:layout_margin="2dp"
android:background="#FFFF66"
android:layout_weight="1"
android:onClick="paintClicked"
android:tag="FFFF66" />
最后这只是我试图从上面的onClick调用的方法:
public void paintClicked(View view) {
Toast.makeText(getApplicationContext(), "Color selected", Toast.LENGTH_LONG).show();
}
我试图在这个方法中加入一些更重要的代码,而不仅仅是吐司,但是现在,调试后我的应用程序甚至没有回到paintClicked方法,我不确定为什么?
03-26 12:12:51.927: E/AndroidRuntime(17686): FATAL EXCEPTION: main
03-26 12:12:51.927: E/AndroidRuntime(17686): java.lang.IllegalStateException: Could not find a method paintClicked(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.ImageButton
03-26 12:12:51.927: E/AndroidRuntime(17686): at android.view.View$1.onClick(View.java:3825)
03-26 12:12:51.927: E/AndroidRuntime(17686): at android.view.View.performClick(View.java:4475)
03-26 12:12:51.927: E/AndroidRuntime(17686): at android.view.View$PerformClick.run(View.java:18786)
03-26 12:12:51.927: E/AndroidRuntime(17686): at android.os.Handler.handleCallback(Handler.java:730)
03-26 12:12:51.927: E/AndroidRuntime(17686): at android.os.Handler.dispatchMessage(Handler.java:92)
03-26 12:12:51.927: E/AndroidRuntime(17686): at android.os.Looper.loop(Looper.java:137)
03-26 12:12:51.927: E/AndroidRuntime(17686): at android.app.ActivityThread.main(ActivityThread.java:5419)
03-26 12:12:51.927: E/AndroidRuntime(17686): at java.lang.reflect.Method.invokeNative(Native Method)
03-26 12:12:51.927: E/AndroidRuntime(17686): at java.lang.reflect.Method.invoke(Method.java:525)
03-26 12:12:51.927: E/AndroidRuntime(17686): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
03-26 12:12:51.927: E/AndroidRuntime(17686): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
03-26 12:12:51.927: E/AndroidRuntime(17686): at dalvik.system.NativeStart.main(Native Method)
03-26 12:12:51.927: E/AndroidRuntime(17686): Caused by: java.lang.NoSuchMethodException: paintClicked [class android.view.View]
03-26 12:12:51.927: E/AndroidRuntime(17686): at java.lang.Class.getConstructorOrMethod(Class.java:423)
03-26 12:12:51.927: E/AndroidRuntime(17686): at java.lang.Class.getMethod(Class.java:787)
03-26 12:12:51.927: E/AndroidRuntime(17686): at android.view.View$1.onClick(View.java:3818)
03-26 12:12:51.927: E/AndroidRuntime(17686): ... 11 more
答案 0 :(得分:0)
您需要为按钮设置ID,然后执行以下操作:
Button button1;
button1 =(Button)optionsDialog.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
Toast.makeText(getApplicationContext(), "Color selected", Toast.LENGTH_LONG).show();
}
将来自paintClicked方法的代码放在onclicklistener中。
在方法中执行此操作的示例:(我尚未对此进行测试):
public void optionsPressed(MenuItem item){
final Dialog optionsDialog = new Dialog(this);
optionsDialog.setTitle("Options");
View dialogView = View.inflate(this, R.layout.options, null);
button1 = (Button) dialogView.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
Toast.makeText(getApplicationContext(), "Color selected", Toast.LENGTH_LONG).show();
}
optionsDialog .setContentView(dialogView);
optionsDialog .show();
}
答案 1 :(得分:0)
android:onClick="paintClicked"
这会在当前活动中搜索paintClicked
,如果在片段或对话框中有此方法,则无法找到它。所以:使用典型的setOnClickListener()并在那里实现你的函数或将此函数移动到活动(不推荐)