我有一个微调器,它首先正确填充选项列表。然后,当我切换活动并返回并点击微调器时,应用程序崩溃(参见下面的日志)。
spinner1 = (Spinner) view.findViewById(R.id.fldWithSpinner1);
ArrayAdapter<?> aa = new ArrayAdapter<Object>(LocationsUI.this,
android.R.layout.simple_spinner_item,
availableLocations);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(aa);
每当我切换到LocationsUI时,都会创建微调器。微调器只在第一次显示LocationsUI时工作正常,之后每次在重新显示LocationsUI后点击微调器时,它都会崩溃:
10-11 22:35:20.060: E/AndroidRuntime(30904): FATAL EXCEPTION: main
10-11 22:35:20.060: E/AndroidRuntime(30904): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@407f6d50 is not valid; is your activity running?
10-11 22:35:20.060: E/AndroidRuntime(30904): at android.view.ViewRoot.setView(ViewRoot.java:538)
10-11 22:35:20.060: E/AndroidRuntime(30904): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:203)
10-11 22:35:20.060: E/AndroidRuntime(30904): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:117)
10-11 22:35:20.060: E/AndroidRuntime(30904): at android.view.Window$LocalWindowManager.addView(Window.java:424)
10-11 22:35:20.060: E/AndroidRuntime(30904): at android.app.Dialog.show(Dialog.java:241)
10-11 22:35:20.060: E/AndroidRuntime(30904): at android.app.AlertDialog$Builder.show(AlertDialog.java:823)
10-11 22:35:20.060: E/AndroidRuntime(30904): at android.widget.Spinner.performClick(Spinner.java:261)
10-11 22:35:20.060: E/AndroidRuntime(30904): at android.view.View$PerformClick.run(View.java:9249)
10-11 22:35:20.060: E/AndroidRuntime(30904): at android.os.Handler.handleCallback(Handler.java:587)
10-11 22:35:20.060: E/AndroidRuntime(30904): at android.os.Handler.dispatchMessage(Handler.java:92)
10-11 22:35:20.060: E/AndroidRuntime(30904): at android.os.Looper.loop(Looper.java:130)
10-11 22:35:20.060: E/AndroidRuntime(30904): at android.app.ActivityThread.main(ActivityThread.java:3691)
10-11 22:35:20.060: E/AndroidRuntime(30904): at java.lang.reflect.Method.invokeNative(Native Method)
10-11 22:35:20.060: E/AndroidRuntime(30904): at java.lang.reflect.Method.invoke(Method.java:507)
10-11 22:35:20.060: E/AndroidRuntime(30904): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
10-11 22:35:20.060: E/AndroidRuntime(30904): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
10-11 22:35:20.060: E/AndroidRuntime(30904): at dalvik.system.NativeStart.main(Native Method)
10-11 22:35:20.080: E/InputDispatcher(139): channel '40abf270 com.sw.android/com.sw.MyLocAppActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x8
10-11 22:35:20.080: E/InputDispatcher(139): channel '40abf270 com.sw.android/com.sw.MyLocAppActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
您能告诉我解决此问题的正确方向吗?
答案 0 :(得分:1)
正如您的Logcat所说:android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@407f6d50 is not valid; is your activity running?
当您显示不再存在的上下文的对话框时,可能会发生这种情况。以下链接可以解释您的错误:http://dimitar.me/android-displaying-dialogs-from-background-threads/
以下是帮助您从同一链接获取代码的代码:
private Handler myHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case DISPLAY_DLG:
if (!isFinishing()) {
showDialog(MY_DIALOG);
}
break;
}
}
};