2011-08-31 93 views
3

在我的啓動畫面中,我已將其設置爲檢測是否啓用了wifi或3g。如果不是,則會出現一個對話框提示用戶退出並打開其中一個。如果它打開,那麼代碼將繼續。我一直在我的logcat中發現有關我的活動泄漏窗口的錯誤。我不知道如何解決這個問題。下面的代碼和logcat。有任何想法嗎?活動已泄露窗口

這裏是我的代碼:

//create alert dialog for wifi and 3g 
connectionDialog = new AlertDialog.Builder(SplashMain.this).create(); 
Log.d(TAG, "dialog created"); 
connectionDialog.setTitle("Wifi or 3G not detected. Please enable either Wifi or 3G"); 
connectionDialog.setButton("Exit", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
      finish(); 
    } 
}); 

wifiHandler = new Handler(); 
setContentView(R.layout.splashscreen); //Make the splash screen load first 
Thread splashScreenTimer = new Thread(){ //create a timer for the splash screen 
    public void run(){  //create a run class 
     Looper.prepare(); //prepare looper 
     try{   //methods within the run class 
      int screenTimer =0; 
      //make it last 3 seconds - create a while loop 
      while(screenTimer <3000){ 
       sleep(100); //100= 1/10th of a second 
       screenTimer = screenTimer +100; 
      } 
      connectionState(); //check wifi stuff 
      Log.d(TAG, "checked wifi state"); 
      if(mobile == true || wifi == true){ 
       Log.d(TAG, "wifi is true"); 
       connectionDialog.dismiss(); 
       startActivity (new Intent("ravebox.dev.sdr.CLEARSCREEN")); 
       finish(); 
       Log.d(TAG, "started activity"); 
      } 
      if(mobile == false || wifi == false){ 
       Log.d(TAG, "wifi is false"); 
       wifiHandler.post(new Runnable() { 
        @Override 
        public void run() { 
         Log.d(TAG, "show dialog"); 
         connectionDialog.show(); 
         Log.d(TAG, "show'd dialog"); 
        } 
       }); 
      }//add activity to the manifest 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     }finally{ 
      //finish(); 
     } 
    } 
}; 
splashScreenTimer.start(); 

登錄貓:

08-30 22:45:32.188: ERROR/WindowManager(334): Activity ravebox.dev.sdr.SplashMain has leaked window [email protected] that was originally added here 
08-30 22:45:32.188: ERROR/WindowManager(334): android.view.WindowLeaked: Activity ravebox.dev.sdr.SplashMain has leaked window [email protected] that was originally added here 
08-30 22:45:32.188: ERROR/WindowManager(334):  at android.view.ViewRoot.<init>(ViewRoot.java:258) 
08-30 22:45:32.188: ERROR/WindowManager(334):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148) 
08-30 22:45:32.188: ERROR/WindowManager(334):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 
08-30 22:45:32.188: ERROR/WindowManager(334):  at android.view.Window$LocalWindowManager.addView(Window.java:465) 
08-30 22:45:32.188: ERROR/WindowManager(334):  at android.app.Dialog.show(Dialog.java:241) 
08-30 22:45:32.188: ERROR/WindowManager(334):  at ravebox.dev.sdr.SplashMain$2$1.run(SplashMain.java:90) 
08-30 22:45:32.188: ERROR/WindowManager(334):  at android.os.Handler.handleCallback(Handler.java:587) 
08-30 22:45:32.188: ERROR/WindowManager(334):  at android.os.Handler.dispatchMessage(Handler.java:92) 
08-30 22:45:32.188: ERROR/WindowManager(334):  at android.os.Looper.loop(Looper.java:123) 
08-30 22:45:32.188: ERROR/WindowManager(334):  at android.app.ActivityThread.main(ActivityThread.java:3835) 
08-30 22:45:32.188: ERROR/WindowManager(334):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-30 22:45:32.188: ERROR/WindowManager(334):  at java.lang.reflect.Method.invoke(Method.java:507) 
08-30 22:45:32.188: ERROR/WindowManager(334):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 
08-30 22:45:32.188: ERROR/WindowManager(334):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 
08-30 22:45:32.188: ERROR/WindowManager(334):  at dalvik.system.NativeStart.main(Native Method) 

回答

7

這裏有一個答案是說明了爲什麼這個問題發生:Activity has leaked window that was originally added

現在,你如果你寫了這個代碼

if(mobile == true || wifi == true){ 
     Log.d(TAG, "wifi is true"); 
     connectionDialog.dismiss(); 
     startActivity (new Intent("ravebox.dev.sdr.CLEARSCREEN")); 
     finish(); 
     Log.d(TAG, "started activity"); 
} 

在上面的代碼中有Y在解散前顯示connectionDialog.dismiss();

而在此代碼中,您顯示的對話框是connectionDialog.show();,但代碼的位置在哪裏?

​​

所以,請找到一個解決方案,它應該是這樣的。

僅在啓動時顯示對話框,如果wifi已連接cancel()它並且移動到下一個活動,如果沒有連接cancel()它在某個時間後發出一條消息,指出wifi沒有找到或連接。

2

我的猜測是因爲你正在更新的線程中的UI。特別是,這個代碼塊:


    if(mobile == true || wifi == true){ 
     Log.d(TAG, "wifi is true"); 
     connectionDialog.dismiss(); 
     startActivity (new Intent("ravebox.dev.sdr.CLEARSCREEN")); 
     finish(); 
     Log.d(TAG, "started activity"); 
    } 

作爲替代,你可能想使用CountDownTimer爲遞減計數,並在onFinish檢查上面的代碼()方法

1

解決方案一:
在完成您的活動之前解僱您的AlertDialog

解決方法二:
添加下面的字段中爲活動的AndroidManifest.xml

android:configChanges="orientation|keyboardHidden|navigation" 
+0

什麼第二方法實際上呢? – aProgrammer

0

發現可能引發異常
我得到了同樣的異常的任何代碼時,「網址」是錯誤的一樣如下:

String url = String.format(SOAP_REGISTER_URL, serviceCenterAddress); 
    HttpTransportSE androidHttpTransport = new HttpTransportSE(url); 

剛剛確立正確的URL運行

相關問題