单击对话框时Android应用程序强制关闭

时间:2014-04-18 11:08:11

标签: android

我点击关闭按钮时出现一个对话框,一切正常,但如果突然有人点击对话框的主体,应用程序将强行关闭

final Dialog dialog = new Dialog(context);
    dialog.setContentView(R.layout.activity_exit);
    dialog.setTitle("closing app");

    ImageView image = (ImageView) dialog.findViewById(R.id.id_exit_image);
    image.setImageResource(R.drawable.ic_launcher);

    Button dialogButtonyes = (Button) dialog.findViewById(R.id.id_exit_yes);
    Button dialogButtonno = (Button) dialog.findViewById(R.id.id_exit_no);
    dialogButtonyes.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
            finish();
        }
    });
    dialogButtonno.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    dialog.show();
};

错误在哪里?

logcat的

FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not find a method title(View) in the activity class     android.view.ContextThemeWrapper for onClick handler on view class android.widget.LinearLayout with id 'LinearLayout1'
at android.view.View$1.onClick(View.java:3825)
at android.view.View.performClick(View.java:4475)
at android.view.View$PerformClick.run(View.java:18786)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5493)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchMethodException: title [class android.view.View]
at java.lang.Class.getConstructorOrMethod(Class.java:423)
at java.lang.Class.getMethod(Class.java:787)
at android.view.View$1.onClick(View.java:3818)

1 个答案:

答案 0 :(得分:1)

在xml中声明单击处理程序方法时会引发异常,但该方法不存在于代码中。