问题
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();
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)
回答1:
Here is an answer that shows why this problem occurs: Activity has leaked window that was originally added
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.
回答2:
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
回答3:
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"
回答4:
find any code that may throws exceptions
i got the same exception when "url" is wrong like followings:
String url = String.format(SOAP_REGISTER_URL, serviceCenterAddress);
HttpTransportSE androidHttpTransport = new HttpTransportSE(url);
just set the right url it runs
来源:https://stackoverflow.com/questions/7252086/activity-has-leaked-window