Android ALerDialog - 无法添加窗口异常

发布于 2024-11-30 01:05:27 字数 3762 浏览 1 评论 0原文

所以基本上我在 TabHost 中使用 TabGroupActivity,这样我就可以在一个选项卡内切换不同的活动。两天前我现在运行的代码没问题,没有任何例外。但在那段时间我正在翻转视图现在我用不同的活动更改所有视图。所以我的情况是这样的:

Main tab Activity -> opens Second activity -> which opens the third activity ,where the AlertDialog is.

我使用的代码是:

Button deactivate = (Button) findViewById(R.id.deactivate);
        deactivate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                new AlertDialog.Builder( getParent() )
                .setTitle( "Warning" )
                .setMessage( "The collection will be removed completely from the device.You can reactivate it later again.This operation requires internet connection." )
                .setPositiveButton( "Go ahead", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Log.d("AlertDialog", "Positive");
                    }
                })
                .setNegativeButton( "Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Log.d("AlertDialog","Negative");
                    }
                })
                .show();    

            }

    });

并且例外是:

08-14 11:08:33.254: ERROR/AndroidRuntime(4726): FATAL EXCEPTION: main
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@463430d0 is not valid; is your activity running?
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at android.view.ViewRoot.setView(ViewRoot.java:509)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at android.app.Dialog.show(Dialog.java:241)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at com.stampii.stampii.mystampii.MyCollectionId$4.onClick(MyCollectionId.java:74)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at android.view.View.performClick(View.java:2408)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at android.view.View$PerformClick.run(View.java:8817)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at android.os.Handler.handleCallback(Handler.java:587)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at android.os.Looper.loop(Looper.java:144)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at android.app.ActivityThread.main(ActivityThread.java:4937)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at java.lang.reflect.Method.invokeNative(Native Method)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at java.lang.reflect.Method.invoke(Method.java:521)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at dalvik.system.NativeStart.main(Native Method)

我尝试使用 thisMyCollecionId.this,添加上下文广告等,但我无法让这些东西发挥作用。有什么想法可以解决这个问题吗?

So basically I'm using a TabGroupActivity inside my TabHost,so I can switch different activities inside of one tab.Two days before the code that I'm running right now was ok,without any exceptions.But in that time I was flipping views inside the tab.Now I change all views with different activities.So my situation is something like that:

Main tab Activity -> opens Second activity -> which opens the third activity ,where the AlertDialog is.

The code I'm using is that :

Button deactivate = (Button) findViewById(R.id.deactivate);
        deactivate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                new AlertDialog.Builder( getParent() )
                .setTitle( "Warning" )
                .setMessage( "The collection will be removed completely from the device.You can reactivate it later again.This operation requires internet connection." )
                .setPositiveButton( "Go ahead", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Log.d("AlertDialog", "Positive");
                    }
                })
                .setNegativeButton( "Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Log.d("AlertDialog","Negative");
                    }
                })
                .show();    

            }

    });

and the Exception is :

08-14 11:08:33.254: ERROR/AndroidRuntime(4726): FATAL EXCEPTION: main
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@463430d0 is not valid; is your activity running?
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at android.view.ViewRoot.setView(ViewRoot.java:509)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at android.app.Dialog.show(Dialog.java:241)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at com.stampii.stampii.mystampii.MyCollectionId$4.onClick(MyCollectionId.java:74)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at android.view.View.performClick(View.java:2408)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at android.view.View$PerformClick.run(View.java:8817)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at android.os.Handler.handleCallback(Handler.java:587)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at android.os.Looper.loop(Looper.java:144)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at android.app.ActivityThread.main(ActivityThread.java:4937)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at java.lang.reflect.Method.invokeNative(Native Method)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at java.lang.reflect.Method.invoke(Method.java:521)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):     at dalvik.system.NativeStart.main(Native Method)

I tried with this, MyCollecionId.this, with adding Context ad etc.but I can't get the things to work.Any ideas how to solve that problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

陈甜 2024-12-07 01:05:27
Button deactivate = (Button) findViewById(R.id.deactivate);
        deactivate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                new AlertDialog.Builder( yourActivity.this )
                .setTitle( "Warning" )
                .setMessage( "The collection will be removed completely from the device.You can reactivate it later again.This operation requires internet connection." )
                .setPositiveButton( "Go ahead", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Log.d("AlertDialog", "Positive");
                    }
                })
                .setNegativeButton( "Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Log.d("AlertDialog","Negative");
                    }
                })
                .show();    

            }

    });

尝试一下,传递您正在运行的活动的上下文

Button deactivate = (Button) findViewById(R.id.deactivate);
        deactivate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                new AlertDialog.Builder( yourActivity.this )
                .setTitle( "Warning" )
                .setMessage( "The collection will be removed completely from the device.You can reactivate it later again.This operation requires internet connection." )
                .setPositiveButton( "Go ahead", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Log.d("AlertDialog", "Positive");
                    }
                })
                .setNegativeButton( "Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Log.d("AlertDialog","Negative");
                    }
                })
                .show();    

            }

    });

try it out, pass your running activity's context

迷离° 2024-12-07 01:05:27

试试这个:

Button deactivate = (Button) findViewById(R.id.deactivate);
        deactivate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            if(!isFinishing()){
                new AlertDialog.Builder( yourActivity.this )
                .setTitle( "Warning" )
                .setMessage( "The collection will be removed completely from the device.You can reactivate it later again.This operation requires internet connection." )
                .setPositiveButton( "Go ahead", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Log.d("AlertDialog", "Positive");
                    }
                })
                .setNegativeButton( "Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Log.d("AlertDialog","Negative");
                    }
                })
                .show();    
               }
            }

    });

try this:

Button deactivate = (Button) findViewById(R.id.deactivate);
        deactivate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            if(!isFinishing()){
                new AlertDialog.Builder( yourActivity.this )
                .setTitle( "Warning" )
                .setMessage( "The collection will be removed completely from the device.You can reactivate it later again.This operation requires internet connection." )
                .setPositiveButton( "Go ahead", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Log.d("AlertDialog", "Positive");
                    }
                })
                .setNegativeButton( "Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Log.d("AlertDialog","Negative");
                    }
                })
                .show();    
               }
            }

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