为什么我无法从一个对话框创建另一个对话框?

发布于 2024-12-19 21:42:49 字数 4002 浏览 2 评论 0原文

在我的应用程序中,我需要打开一个对话框。在按下该对话框上的按钮时,我想打开另一个对话框并执行一些操作。 这两个对话框都是自定义对话框。 但是,当我这样做时,我无法创建对话框...

请参见下面的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

永不分离 2024-12-26 21:42:50
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); 
Context context;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout......);

        context = this;
    }

Dialog inside dialog:

    aboutDialog = new Dialog(context, R.style.CustomDialogTheme);             
            aboutDialog.setContentView(R.layout.about_dialog);        
            aboutDialog.setCancelable(true); 
天涯离梦残月幽梦 2024-12-26 21:42:49

使用当前活动作为上下文:

aboutDialog = new Dialog(ActivityName.this,R.style.CustomDialogTheme);

Use the current activity as context:

aboutDialog = new Dialog(ActivityName.this,R.style.CustomDialogTheme);
红ご颜醉 2024-12-26 21:42:49

我认为不要在按钮第二个对话框打开代码中使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文