Activity has leaked window

In my splash screen, I made it so that it detects whether wifi or 3g is enabled or not. If it is not, a dialog screen prompts the user to exit and turn either one on. If it is on, then the code will continue. I keep getting an error in my logcat about my activity having a leaked window. I am not sure how to solve this issue. Code and logcat below. Any Ideas?

Here's my code:

/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();
        //Make the splash screen load first
        setContentView(R.layout.splashscreen);
        //create a timer for the splash screen
        Thread splashScreenTimer = new Thread(){
            //create a run class
            public void run(){
                //prepare looper
                Looper.prepare();
                //methods within the run class
                try{
                    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;
                    }
                    //check wifi stuff
                    connectionState();
                    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
                //startActivity (new Intent("ravebox.dev.sdr.CLEARSCREEN"));
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    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)
link | improve this question

 
feedback

3 Answers

up vote 1 down vote accepted

Here is an answer that shows why this problem occurs,

StackOverflow

Now, in you case you have written this code

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

                    }

In the above code where are you showing the connectionDialog.dismiss(); before dismissing it.

And in this code you are showing the dialog by connectionDialog.show(); but where is the code to dismiss it.

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

So, please find a solution for this, it should be something like this.

Show the dialog in starting only, if the wifi is connected cancel() it and move to next activity, if not connected cancel() it after sometime and giving a message that wifi not found or connected.

link | improve this answer
 
feedback

My guess is because you are updating the UI within the Thread. In particular, this block of code:


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

As an alternative you might want to use CountDownTimer for counting down and check the above code at onFinish() method

link | improve this answer
 
feedback

Solution one:
dismiss your AlertDialog before finish your Activity.

AlertDialog.dismiss();

Solution two:
Add the following field for your Activity in AndroidManifest.xml.

android:configChanges="orientation|keyboardHidden|navigation"
link | improve this answer
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值