2012-10-12 78 views
0

我有一個微調器,它首先正確填充選項列表。然後,當我切換活動並返回並點擊微調器時,應用程序崩潰(請參閱下面的日誌)。Android微調器在敲擊時崩潰

 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 [email protected] 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! 

能否請你告訴我正確的方向解決這個問題?

回答

1

隨着你的logcat說:android.view.WindowManager$BadTokenException: Unable to add window -- token [email protected] 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; 
    } 
    } 
}; 
+0

好文章,感謝您的鏈接。我接受你的答案,因爲它向我展示了正確的方向。 – Rexb

+0

@Rexb謝謝..... –