我在应用程序中进行开发,我正在使用一个活动,我通过使用线程逐页显示列表视图项目。当用户选择项目时,它将把用户带到另一个活动。
但是有些时候,当我点击列表视图项时,应用程序崩溃了。它在log cat中说了些什么,但我无法理解它。
这是堆栈跟踪:
06-24 16:26:44.320: ERROR/AndroidRuntime(4248): FATAL EXCEPTION: main
06-24 16:26:44.320: ERROR/AndroidRuntime(4248): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@4073ef78 is not valid; is your activity running?
06-24 16:26:44.320: ERROR/AndroidRuntime(4248): at android.view.ViewRoot.setView(ViewRoot.java:527)
06-24 16:26:44.320: ERROR/AndroidRuntime(4248): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
06-24 16:26:44.320: ERROR/AndroidRuntime(4248): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
06-24 16:26:44.320: ERROR/AndroidRuntime(4248): at android.view.Window$LocalWindowManager.addView(Window.java:424)
06-24 16:26:44.320: ERROR/AndroidRuntime(4248): at android.app.Dialog.show(Dialog.java:241)
06-24 16:26:44.320: ERROR/AndroidRuntime(4248): at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
06-24 16:26:44.320: ERROR/AndroidRuntime(4248): at com.usablenet.mobile.walgreen.app.util.ErrorAlertServiceImp.showErrorAlert(ErrorAlertServiceImp.java:86)
06-24 16:26:44.320: ERROR/AndroidRuntime(4248): at com.usablenet.mobile.walgreen.pharmacy.PrescriptionList.showErrorCodeAlert(PrescriptionList.java:360)
06-24 16:26:44.320: ERROR/AndroidRuntime(4248): at com.usablenet.mobile.walgreen.pharmacy.PrescriptionList.access$300(PrescriptionList.java:45)
06-24 16:26:44.320: ERROR/AndroidRuntime(4248): at com.usablenet.mobile.walgreen.pharmacy.PrescriptionList$2.handleMessage(PrescriptionList.java:323)
06-24 16:26:44.320: ERROR/AndroidRuntime(4248): at android.os.Handler.dispatchMessage(Handler.java:99)
06-24 16:26:44.320: ERROR/AndroidRuntime(4248): at android.os.Looper.loop(Looper.java:130)
06-24 16:26:44.320: ERROR/AndroidRuntime(4248): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-24 16:26:44.320: ERROR/AndroidRuntime(4248): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 16:26:44.320: ERROR/AndroidRuntime(4248): at java.lang.reflect.Method.invoke(Method.java:507)
06-24 16:26:44.320: ERROR/AndroidRuntime(4248): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-24 16:26:44.320: ERROR/AndroidRuntime(4248): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-24 16:26:44.320: ERROR/AndroidRuntime(4248): at dalvik.system.NativeStart.main(Native Method)
谢谢, 巴鲁。
答案 0 :(得分:1)
对AlertDialog使用getApplicationContext
,否则在任务之间切换时会出现此错误。
而不是:AlertDialog.Builder builder = new AlertDialog.Builder(this);
使用方法:ConnectionDetector = new ConnectionDetector(getApplicationContext());
对话框中的任何听众也是如此。
此外:
if (!ConnectionDetector.isConnectingToInternet()) {
alert.showAlertDialog(ListActivity.this, "Internet Connection Error","Please connect to working Internet connection", false);
// stop executing code by return
return;
}