IllegalArgumentException: View not attached to window manager . Dialog.dismiss
I have a little error i want to get rid of. I have no idea why this error occurs. The simulator and test phone runs perfect! The only info I have is the Stacktraces I got from the app users in android Market.
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:432)
at android.app.Dialog.dismissDialog(Dialog.java:278)
at android.app.Dialog.access$000(Dialog.java:71)
at android.app.Dialog$1.run(Dialog.java:111)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invok开发者_StackOverflow中文版e(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method
First i thought the code below would cause the application to crash if the user change the phone's orientation while the progress dialog is not yet dismissed. I made shure the orientation would not change by adding: android:screenOrientation="portrait". But the error is still alive.
Could any one help me out? This is a code example which i use:
final ProgressDialog pd = ProgressDialog.show(this, "Title", "Message", true, false);
new Thread(new Runnable(){
public void run(){
makeHttpRequest();
pd.dimiss();
}
}).start();
Did you put android:screenOrientation="portrait"
in activity or in appication? also this exception occurs when dialog is not properly dismiss, when activity stops, means you should handle dialog dismiss in onPause/ onStop. I have face this issue once.
Even if you restrict the orientation to portrait, the lock screen for example might get into landscape and your activity is recreated. In order to make sure that the dialog gets dismissed, I would do this:
if (dialog != null && dialog.isShowing()) {
dialog.cancel();
}
Also, you should dismiss the dialog when the activity is destroyed:
@Override
protected void onDestroy() {
if (dialog != null && dialog.isShowing()) {
dialog.cancel();
}
super.onDestroy();
}
This solved the problem for me - hope it helps :)
Try this code:
update.setTitle(getResources().getString(R.string.app_name));
update.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
update.setCancelable(true);
update.setMax(100);
update.show();
Thread background = new Thread (new Runnable() {
public void run() {
try {
// enter the code to be run while displaying the progressbar.
//
// This example is just going to increment the progress bar:
// So keep running until the progress value reaches maximum value
while (update.getProgress()<= update.getMax()) {
// wait 500ms between each update
Thread.sleep(500);
// active the update handler
progressHandler.sendMessage(progressHandler.obtainMessage());
}
} catch (java.lang.InterruptedException e) {
// if something fails do something smart
}
}
});// start the background thread
background.start();
if(update.getProgress()== 100)
{
update.dismiss();
}
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.show();
精彩评论