在我的应用程序中,我需要打开一个对话框。按下该对话框上的按钮,我想打开另一个对话框并执行一些操作。 对话框都是自定义对话框。 但是当我这样做时,我无法创建对话框......
见下面的代码:
twsbiImageDialog = new Dialog(this,R.style.CustomDialogTheme);
twsbiImageDialog.setContentView(R.layout.twsbi_logo_dialog);
twsbiImageDialog.setCancelable(true);
// To visit Website ===================================
Button visitWebsiteButton = (Button) twsbiImageDialog.findViewById(R.id.visitWebsiteButton);
visitWebsiteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
twsbiImageDialog.dismiss();
Intent in = new Intent(FingerPaint.this,GoogleSite.class);
startActivity(in);
}
});
// For About ===================================
Button aboutButton = (Button) twsbiImageDialog.findViewById(R.id.aboutButton);
aboutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
twsbiImageDialog.dismiss();
//System.out.println("I Am here before Dialog");
aboutDialog = new Dialog(getApplicationContext(),R.style.CustomDialogTheme);
aboutDialog.setContentView(R.layout.about_dialog);
aboutDialog.setCancelable(true);
Button okButton = (Button) aboutDialog.findViewById(R.id.okButton);
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
aboutDialog.dismiss();
}
});
aboutDialog.show();
//openAboutDialog();
}
});
twsbiImageDialog.show();
如果我喜欢,我会得到如下错误:
12-05 11:43:21.568: ERROR/AndroidRuntime(545): FATAL EXCEPTION: main
12-05 11:43:21.568: ERROR/AndroidRuntime(545): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
12-05 11:43:21.568: ERROR/AndroidRuntime(545): at android.view.ViewRoot.setView(ViewRoot.java:509)
12-05 11:43:21.568: ERROR/AndroidRuntime(545): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
12-05 11:43:21.568: ERROR/AndroidRuntime(545): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
12-05 11:43:21.568: ERROR/AndroidRuntime(545): at android.app.Dialog.show(Dialog.java:241)
12-05 11:43:21.568: ERROR/AndroidRuntime(545): at com.project.twsbi.FingerPaint$8.onClick(FingerPaint.java:546)
12-05 11:43:21.568: ERROR/AndroidRuntime(545): at android.view.View.performClick(View.java:2408)
12-05 11:43:21.568: ERROR/AndroidRuntime(545): at android.view.View$PerformClick.run(View.java:8816)
12-05 11:43:21.568: ERROR/AndroidRuntime(545): at android.os.Handler.handleCallback(Handler.java:587)
12-05 11:43:21.568: ERROR/AndroidRuntime(545): at android.os.Handler.dispatchMessage(Handler.java:92)
12-05 11:43:21.568: ERROR/AndroidRuntime(545): at android.os.Looper.loop(Looper.java:123)
12-05 11:43:21.568: ERROR/AndroidRuntime(545): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-05 11:43:21.568: ERROR/AndroidRuntime(545): at java.lang.reflect.Method.invokeNative(Native Method)
12-05 11:43:21.568: ERROR/AndroidRuntime(545): at java.lang.reflect.Method.invoke(Method.java:521)
12-05 11:43:21.568: ERROR/AndroidRuntime(545): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-05 11:43:21.568: ERROR/AndroidRuntime(545): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-05 11:43:21.568: ERROR/AndroidRuntime(545): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
我认为不要在按钮第二个对话框打开代码中使用 getApplicationContext()。使用此页面的上下文。
试试这个,它可能对你有帮助。
答案 1 :(得分:2)
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout......);
context = this;
}
对话框内的对话框:
aboutDialog = new Dialog(context, R.style.CustomDialogTheme);
aboutDialog.setContentView(R.layout.about_dialog);
aboutDialog.setCancelable(true);
答案 2 :(得分:2)
将当前活动用作上下文:
aboutDialog = new Dialog(ActivityName.this,R.style.CustomDialogTheme);