为什么我无法从一个对话框创建另一个对话框?
在我的应用程序中,我需要打开一个对话框。在按下该对话框上的按钮时,我想打开另一个对话框并执行一些操作。 这两个对话框都是自定义对话框。 但是,当我这样做时,我无法创建对话框...
请参见下面的代码:
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)
In My application i need to open one dialog. And on press of button on that dialog i want to open another dialog and do some action.
Both the dialog are custom dialog.
But while i do that i am not able to create the dialog...
See below code:
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();
And If i do like that i got the error like below:
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对话框内的对话框:
Dialog inside dialog:
使用当前活动作为上下文:
Use the current activity as context:
我认为不要在按钮第二个对话框打开代码中使用 getApplicationContext() 。使用此页面的上下文。
试试这个,也许会对你有帮助。
I think dont use getApplicationContext() in your button second dialog open code. Use the context of this page.
Try this, It may be help you.