我有一个Activity
和很多Fragment
个。我用hide
& show
。第一次一切正常,但当我退出应用程序并重新启动应用程序时崩溃。
以下是异常消息:
[ 05-10 10:47:12.561 15134:15134 E/ ]
android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@30940158 is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:606)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:311)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.app.Dialog.show(Dialog.java:325)
at ruiming.com.medicalmanagement.view.MainActivity.showProgressDialog(MainActivity.java:447)
at ruiming.com.medicalmanagement.view.PatientsCases.loadList(PatientsCases.java:106)
at ruiming.com.medicalmanagement.view.PatientsCases.onViewCreated(PatientsCases.java:92)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:908)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1069)
at android.app.BackStackRecord.run(BackStackRecord.java:834)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1454)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5689)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
这是progressDialog
show
和hide
个功能:
private static ProgressDialog progressDialog;
public static void showProgressDialog(Activity mActivity) {
if(progressDialog == null){
progressDialog = ProgressDialog.show(mActivity, "Please wait", "Loading...", true, false);
}
if(!mActivity.isFinishing() && !progressDialog.isShowing()){
progressDialog.show();
}
}
public static void hideProgressDialog() {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
这些功能在活动中。
这是我的片段代码:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MainActivity.showProgressDialog(getActivity());
new Thread(new Runnable() {
@Override
public void run() {
mList = MyJDBCUtil.getPrAcupuntrueCode(getActivity());
try {
Thread.sleep(500);
EventBus.getDefault().post(new EventBean.PrDiagonsisEvent("3"));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
@Subscribe(threadMode = ThreadMode.MAIN, priority = 0, sticky = false)
public void refreshList(EventBean.PrDiagonsisEvent pr) {
if (pr.getMsg().equals("3")) {
adapter = new TreatmentAdapter(getActivity());
lvTreatmentList.setAdapter(adapter);
MainActivity.hideProgressDialog();
}
}
我不知道我的代码有什么问题。有什么建议吗?
答案 0 :(得分:0)
在你正在呼唤的showProgressDialog()
中:
progressDialog.show();
两次..
当您为不再存在的上下文显示dialog
时,会发生错误。
所以将代码修改为..
public static void showProgressDialog(Activity mActivity) {
if((progressDialog == null) && (!mActivity.isFinishing())){
progressDialog = ProgressDialog.show(mActivity, "Please wait", "Loading...", true, false);
}
if(!mActivity.isFinishing() && !progressDialog.isShowing()){
progressDialog.show();
}
}