我正在尝试打开一个对话框窗口,但每次我尝试打开它都会抛出此异常:
Uncaught handler: thread main exiting due to uncaught exception
android.view.WindowManager$BadTokenException:
Unable to add window -- token null is not for an application
at android.view.ViewRoot.setView(ViewRoot.java:460)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.app.Dialog.show(Dialog.java:238)
at android.app.Activity.showDialog(Activity.java:2413)
Run Code Online (Sandbox Code Playgroud)
我是通过showDialog显示器的id 调用来创建的.该onCreateDialog处理器记录良好,我可以通过它没有问题的一步,但因为它看起来像我想的东西我已经把它贴吧:
@Override
public Dialog onCreateDialog(int id)
{
Dialog dialog;
Context appContext = this.getApplicationContext();
switch(id)
{
case RENAME_DIALOG_ID:
Log.i("Edit", "Creating rename dialog...");
dialog = new Dialog(appContext);
dialog.setContentView(R.layout.rename);
dialog.setTitle("Rename " + noteName);
break;
default:
dialog = null;
break;
}
return dialog;
}
Run Code Online (Sandbox Code Playgroud)
这有什么不足之处吗?有些问题在创建对话框时已经讨论过这个问题onCreate …
android runtimeexception android-dialog android-windowmanager
我似乎无法理解为什么会这样.这段代码:
mProgressDialog = ProgressDialog.show(this, "", getString(R.string.loading), true);
Run Code Online (Sandbox Code Playgroud)
工作得很好.但是,这段代码:
mProgressDialog = ProgressDialog.show(getApplicationContext(), "", getString(R.string.loading), true);
Run Code Online (Sandbox Code Playgroud)
抛出以下异常:
W/WindowManager( 569): Attempted to add window with non-application token WindowToken{438bee58 token=null}. Aborting.
D/AndroidRuntime( 2049): Shutting down VM
W/dalvikvm( 2049): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
E/AndroidRuntime( 2049): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 2049): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tastekid.TasteKid/com.tastekid.TasteKid.YouTube}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
E/AndroidRuntime( 2049): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
E/AndroidRuntime( 2049): at …Run Code Online (Sandbox Code Playgroud) 我正在开发一个具有多种活动和服务的Android应用程序.我正在导入到我的项目中的第三方库中定义了一些活动,问题是在某些设备上(特别是在三星Galaxy Tabs中)我的应用程序在从一个活动切换到之前的活动时通过点击返回时保持崩溃按钮.
我做了一个LogCat,发现这是崩溃的原因:
android.view.WindowManager$BadTokenException: Unable to add window -- token
android.os.BinderProxy@351c808e is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:562)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:272)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3017)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Run Code Online (Sandbox Code Playgroud)
但有趣的是,我的应用程序的类没有出现在堆栈跟踪上.我在StackOverflow上搜索了类似的问题,但是我发现其他每个报告都有一个BadTokenException的堆栈跟踪,相应的应用程序代码总是出现在堆栈跟踪中.
有谁知道这个问题的常见原因和/或排除故障的最佳方法是什么?我注意到这个问题在三星设备上发生的更多,所以可能是这些设备上的实际错误.