我有一个小错误想要摆脱。我不知道为什么会发生此错误。模拟器和测试手机运行完美!我拥有的唯一信息是我从android市场中的应用程序用户那里获得的堆栈跟踪。
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.invoke(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
首先,我认为如果用户在进度对话框尚未关闭的情况下更改手机的方向,下面的代码会导致应用程序崩溃。我通过添加:android:screenOrientation="portrait"使舒尔的方向不会改变。但错误仍然存在。
有人能帮我吗?这是我使用的代码示例:
final ProgressDialog pd = ProgressDialog.show(this, "Title", "Message", true, false);
new Thread(new Runnable(){
public void run(){
makeHttpRequest();
pd.dimiss();
}
}).start();
您是将android:screenOrientation="portrait"
置于活动还是应用程序中? 此外,当对话框未正确关闭时,当活动停止时,也会发生此异常,这意味着您应该在 onPause/onStop 中处理对话框关闭。我曾经遇到过这个问题。
即使您将方向限制为纵向,例如锁定屏幕也可能会进入横向状态并重新创建您的活动。为了确保对话框被关闭,我会这样做:
if (dialog != null && dialog.isShowing()) {
dialog.cancel();
}
此外,当活动被销毁时,您应该关闭该对话框:
@Override
protected void onDestroy() {
if (dialog != null && dialog.isShowing()) {
dialog.cancel();
}
super.onDestroy();
}
这为我解决了问题 - 希望它对:)有所帮助
试试这段代码:
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();