1

我有一个Dialog我想设法关闭它的方法ImageButton,我尝试使用dismiss()cancel()方法,但它们在出现的方法中不存在,所以我尝试的是下面的代码:

buttonDialogClose = (ImageButton) findViewById(R.id.buttonCloseDialog);
    buttonDialogClose.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            finish();  
        }
    });

但不幸的是,它显示了一个异常,即:

11-09 11:44:25.696: E/AndroidRuntime(649): FATAL EXCEPTION: main
11-09 11:44:25.696: E/AndroidRuntime(649): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lavan/com.example.lavan.LavanMainActivity}: java.lang.NullPointerException
11-09 11:44:25.696: E/AndroidRuntime(649):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-09 11:44:25.696: E/AndroidRuntime(649):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-09 11:44:25.696: E/AndroidRuntime(649):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-09 11:44:25.696: E/AndroidRuntime(649):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-09 11:44:25.696: E/AndroidRuntime(649):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 11:44:25.696: E/AndroidRuntime(649):  at android.os.Looper.loop(Looper.java:123)
11-09 11:44:25.696: E/AndroidRuntime(649):  at android.app.ActivityThread.main(ActivityThread.java:3683)
11-09 11:44:25.696: E/AndroidRuntime(649):  at java.lang.reflect.Method.invokeNative(Native Method)
11-09 11:44:25.696: E/AndroidRuntime(649):  at java.lang.reflect.Method.invoke(Method.java:507)
11-09 11:44:25.696: E/AndroidRuntime(649):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-09 11:44:25.696: E/AndroidRuntime(649):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-09 11:44:25.696: E/AndroidRuntime(649):  at dalvik.system.NativeStart.main(Native Method)
11-09 11:44:25.696: E/AndroidRuntime(649): Caused by: java.lang.NullPointerException
11-09 11:44:25.696: E/AndroidRuntime(649):  at com.example.lavan.LavanMainActivity.onCreate(LavanMainActivity.java:92)
11-09 11:44:25.696: E/AndroidRuntime(649):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-09 11:44:25.696: E/AndroidRuntime(649):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-09 11:44:25.696: E/AndroidRuntime(649):  ... 11 more

这是我的Dialog代码:

tv5 = (TextView) findViewById(R.id.tvSearchSD);
    tv5.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Dialog dialog = new Dialog(LavanMainActivity.this);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.search_dialog_layout);
            dialog.show();

        }
    });

有人解释它的原因以及我该如何解决它,非常感谢。

4

2 回答 2

0

您的 buttonDialogClose 应该使用 Dialog 进行初始化。在你的情况下。

tv5 = (TextView) findViewById(R.id.tvSearchSD);
    tv5.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Dialog dialog = new Dialog(LavanMainActivity.this);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.search_dialog_layout);
            Button buttonDialogClose = (Button) dialog.findViewById(R.id.buttonDialogClose);
            buttonDialogClose .setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
              dialog.dismiss();
          }
            });
            dialog.show();

        }
    });       
于 2013-11-09T08:30:50.403 回答
0

使用 AlertDialog 而不是 Dialog。这是 alertDialog 的示例代码。

private class myCustomAlertDialog extends AlertDialog {

    protected myCustomAlertDialog(Context context) {
        super(context);

        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        View view = inflater.inflate(R.layout.search_dialog_layout, null);
        ImageButton buttonDialogClose = (ImageButton)view.findViewById(R.id.buttonCloseDialog);
        setView(view);
        buttonDialogClose.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // I want the dialog to close at this point
                dismiss();
            }
        });
    }

}

实例化它并显示它

new myCustomAlertDialog(LavanMainActivity.this).show();
于 2013-11-09T08:35:48.903 回答