昨天有一位用户给我发送了这份崩溃报告:
android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@4250d6d8 is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:698)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
at android.view.Window$LocalWindowManager.addView(Window.java:552)
at android.app.Dialog.show(Dialog.java:277)
at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:833)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
我搜索了一下,有人说解决方法是将this
改为Activity.this
。但即使我使用以下代码,这个解决方法也没有起作用:
//Oncreate Method.....
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
AlertDialog.Builder alertd = new AlertDialog.Builder(MainActivity.this);
alertd.setMessage("StackOverflow");
alertd.setTitle("Example");
AlertDialog alertd2 = alertd.create();
alertd2.show();
}
});
我需要改变一些什么吗?