活动已经泄露窗口

时间:2011-08-31 02:57:01

标签: android android-activity window

在我的启动画面中,我做了它,以便检测是否启用了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();

Log cat:

08-30 22:45:32.188: ERROR/WindowManager(334): Activity ravebox.dev.sdr.SplashMain has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405377e0 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 com.android.internal.policy.impl.PhoneWindow$DecorView@405377e0 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)

4 个答案:

答案 0 :(得分: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");
}

在上面的代码中,您在解除它之前显示connectionDialog.dismiss();

在此代码中,您将通过connectionDialog.show();显示对话框,但是解除它的代码在哪里。

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");
      }
});

所以,请找到一个解决方案,它应该是这样的。

仅在启动时显示对话框,如果wifi已连接cancel()它并移动到下一个活动,如果在一段时间后没有连接cancel()并发出无法找到或连接wifi的消息。 / p>

答案 1 :(得分:2)

我的猜测是因为您正在更新Thread中的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()方法中检查上述代码

答案 2 :(得分:1)

解决方案一:
在完成您的活动之前解雇您的AlertDialog

AlertDialog.dismiss();

解决方案二:
AndroidManifest.xml

中为您的活动添加以下字段
android:configChanges="orientation|keyboardHidden|navigation"

答案 3 :(得分:0)

找到可能引发异常的任何代码
当“url”出现错误时,我得到了同样的例外:

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

只需设置正确运行的网址