我的andorid应用程序在大多数情况下工作正常,但有时在手机旋转时崩溃,我得到下面的错误日志,我不明白。任何人都可以解释为什么会这样吗?
以下是错误日志:
02-22 14:44:52.175: D/AndroidRuntime(26784): Shutting down VM
02-22 14:44:52.175: W/dalvikvm(26784): threadid=1: thread exiting with uncaught exception (group=0x40e81300)
02-22 14:44:52.183: E/AndroidRuntime(26784): FATAL EXCEPTION: main
02-22 14:44:52.183: E/AndroidRuntime(26784): java.lang.IllegalArgumentException: View not attached to window manager
02-22 14:44:52.183: E/AndroidRuntime(26784): at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:653)
02-22 14:44:52.183: E/AndroidRuntime(26784): at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:349)
02-22 14:44:52.183: E/AndroidRuntime(26784): at android.view.WindowManagerImpl$CompatModeWrapper.removeView(WindowManagerImpl.java:160)
02-22 14:44:52.183: E/AndroidRuntime(26784): at android.app.Dialog.dismissDialog(Dialog.java:319)
02-22 14:44:52.183: E/AndroidRuntime(26784): at android.app.Dialog.dismiss(Dialog.java:302)
02-22 14:44:52.183: E/AndroidRuntime(26784): at com.example.chartviewer.JsonActivity$getChartItems.onPostExecute(JsonActivity.java:267)
02-22 14:44:52.183: E/AndroidRuntime(26784): at com.example.chartviewer.JsonActivity$getChartItems.onPostExecute(JsonActivity.java:1)
02-22 14:44:52.183: E/AndroidRuntime(26784): at android.os.AsyncTask.finish(AsyncTask.java:631)
02-22 14:44:52.183: E/AndroidRuntime(26784): at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-22 14:44:52.183: E/AndroidRuntime(26784): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-22 14:44:52.183: E/AndroidRuntime(26784): at android.os.Handler.dispatchMessage(Handler.java:99)
02-22 14:44:52.183: E/AndroidRuntime(26784): at android.os.Looper.loop(Looper.java:137)
02-22 14:44:52.183: E/AndroidRuntime(26784): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-22 14:44:52.183: E/AndroidRuntime(26784): at java.lang.reflect.Method.invokeNative(Native Method)
02-22 14:44:52.183: E/AndroidRuntime(26784): at java.lang.reflect.Method.invoke(Method.java:511)
02-22 14:44:52.183: E/AndroidRuntime(26784): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-22 14:44:52.183: E/AndroidRuntime(26784): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-22 14:44:52.183: E/AndroidRuntime(26784): at dalvik.system.NativeStart.main(Native Method)
更新
以下是onPostExecuteMethod的代码:
//在获取数据时删除进度对话框
protected void onPostExecute(String args) {
progressDialog.dismiss();
//Shows alert dialog if data is unavailable
if(args != null && args.equals(noData)){
AlertDialog.Builder builder = new AlertDialog.Builder(JsonActivity.this);
builder.setIcon(R.drawable.artistlogo);
builder.setTitle("Musicmetric Charts");
builder.setMessage(noData);
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// finishes the activity and leads back to the list of charts
JsonActivity.this.finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
答案 0 :(得分:4)
这些是您的堆栈跟踪中的两个相关行:
com.example.chartviewer.JsonActivity$getChartItems.onPostExecute(JsonActivity.java:267)
您已经实现了AsyncTask的onPostExcecute方法。此方法正在关闭对话框:
android.app.Dialog.dismiss(Dialog.java:302)
当您旋转设备时,AsyncTask继续运行并尝试隐藏不再可见的对话框。
答案 1 :(得分:0)
扩展ashtom的答案:尝试自己维持设备轮换:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.equals(Configuration.ORIENTATION_LANDSCAPE)){
// TODO
}else if (newConfig.equals(Configuration.ORIENTATION_PORTRAIT)){
// TODO
}
}