WindowManager $ BadTokenException AlertDialog

时间:2014-09-12 09:56:25

标签: android crash android-alertdialog

两位用户向我发送了以下错误报告。

首先在Android 2.3.7:

android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@4051c190 is not valid; is your activity running?
at android.view.ViewRoot.setView(ViewRoot.java:527)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:180)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:92)
at android.view.Window$LocalWindowManager.addView(Window.java:424)
at android.app.Dialog.show(Dialog.java:244)
at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:533)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3714)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:853)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
at dalvik.system.NativeStart.main(Native Method)

Android 4.0 ++的第二个

android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@4132f030 is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:646)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:281)
at android.app.AlertDialog$Builder.show(AlertDialog.java:951)
at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:644)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)

似乎我使用alertdialog的方式是假的.... 有人能告诉我使用alertdialog的最佳方法吗?

我的简单代码:

    public class MainActivity extends Activity {

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



 exam.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
                        alert.setMessage("Simple");
                        alert.setTitle("AlertDialog");
                        alert.setPositiveButton("OK", null);
                        alert.show();


                }

                });



    }
    }

0 个答案:

没有答案