我有一个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();
}
});
有人解释它的原因以及我该如何解决它,非常感谢。