我有一个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();
}
});
任何人都可以解释它为什么以及如何解决它,非常感谢。
答案 0 :(得分: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();
}
});
答案 1 :(得分: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();