Activity has leaked window

379653 ワード

1 down vote favorite
share [g+] share [fb] share [tw]
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) 

android  activity  window
link
| improve this question
asked 
Aug 31 '11 at 2:57
SergioRa
201
9
96% accept rate
 
feedback

3 Answers


active oldest votes
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
answered 
Aug 31 '11 at 4:52
Lalit Poptani
12.9k
3
7
33
 
feedback
up vote
2 down vote
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
answered 
Aug 31 '11 at 3:41
momo
3,660
1
6
17
 
feedback
up vote
0 down vote
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