Error: View not attached to window manager
I am getting the following remotely from clients so I don't know what hardware etc they are using.
java.lang.IllegalArgumentException: View not attached to window manager
at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:355)
at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:200)
at android.view.Window$LocalWindowManager.removeView(Window.java:417)
at android.app.Dialog.dismissDialog(Dialog.java:279)
at android.app.Dialog.access$000(Dialog.java:72)
at android.app.Dialog$1.run(Dialog.java:108)
at android.app.Dialog.dismiss(Dialog.java:263)
at com..mysite.android.ActivityGame$1.onFinish(ActivityGame.java:154)
at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:118)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4203)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
This is happening because of a ProgressDialog
progressDialog = new ProgressDialog( this );
progressDialog.setMessage(getString(R.string.get_ready));
progress开发者_StackOverflow社区Dialog.setCancelable(false);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMax(12);
progressDialog.show();
new CountDownTimer(3000, 250) {
@Override
public void onTick(long millisUntilFinished) {
//progressDialog.incrementProgressBy(1);
}
@Override
public void onFinish() {
progressDialog.dismiss(); //********* ERROR HAPPENS HERE *********
nextQuestion();
}
}.start();
The Activity looks like this in the Manifest.
<activity android:theme="@style/GameTheme" android:name=".ActivityGame" android:screenOrientation="portrait" android:launchMode="singleTask" android:configChanges="keyboardHidden|orientation"></activity>
So what could this mean? I think it has something to do with the Activity being destroyed then created but as you can see I have the configChanges set correctly.
Try:
if (pDialog.isShowing()) {
pDialog.cancel();
}
in your overridden onDestroy()
or onStop()
methods.
This problem arises when trying to show a dialog after you've exited an Activity
.
I just solved this problem just by writing down the following code:
public void onDestroy(){
super.onDestroy();
if(progressDialog!=null)
if(progressDialog.isShowing()){
progressDialog.cancel();
}
}
Basically, from which class you started progressDialog
, override onDestroy()
method and do this way. It solved Activity has leaked window problem.
It usually happens when you call dismiss after activity has been closed.
In order to handle invisible views, you cannot use isShowing() and should check for the window attachment itself in your onDestroy()
if (pDialog.getWindowToken() != null)
{
pDialog.dismiss();
}
or in my case, I was not using a dialog but a custom window added by the WindowManager, which was (possibly) invisible.
if (test_service_overlay != null) {
if (test_service_overlay.getWindowToken() != null) {
WindowManager windowManager = (WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE);
windowManager.removeViewImmediate(test_service_overlay);
}
}
Im also faced the same problem when I tried to dismiss the dialog at onPageFinished method of Web view. sometimes onPageFinished called after the activity has been closed.
Here is the solution for it:
if(pDialog.isShowing())
{
try
{
pDialog.dismiss();
}
catch(Exception e) {// nothing }
}
Try it!
You can ask "dialog.isIndeterminate()" as well. Works fine.
精彩评论