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运行

相关问题