活动已经泄露窗口

Spl*_*usa 3 android window android-activity

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

原木猫:

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)
Run Code Online (Sandbox Code Playgroud)

Lal*_*ani 7

这是一个答案,显示出现此问题的原因:Activity已泄漏最初添加的窗口

现在,在您的情况下,您已编写此代码

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");
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中你在哪里显示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");
      }
});
Run Code Online (Sandbox Code Playgroud)

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

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