0

我有一个微调器,一开始可以正确填充选项列表。然后,当我切换活动并返回并点击微调器时,应用程序崩溃(请参阅下面的日志)。

        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!

你能告诉我解决这个问题的正确方向吗?

4

1 回答 1

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;
    }
  }
};
于 2012-10-12T04:46:25.670 回答