所以,我有这段代码,我在其中执行 HttpPost 并等待返回给处理程序的响应。在处理程序块中,我显示了一个 ProgressDialog。在我使用此代码重新启动活动之前,一切似乎都正常
Intent intent = getIntent();
finish();
startActivity(intent);
第一个处理程序的代码是这个
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (0 < msg.what) {
} else {
if (msg.getData() != null) {
String s = msg.getData().getString(getResources().getString(R.string.BundleTextKey));
if (s!=null) {
try{
POSTMethods post_obj = new POSTMethods(messageHandleralert,context);
String sender = ValuesHolder.user;
mprogDialog = ProgressDialog.show(Tab_Activity.this, "", "creating pending request");
post_obj.createPendingRequest(sender);
}
catch(Exception e){
e.printStackTrace();
Toast.makeText(Tab_Activity.this, "Try Again", Toast.LENGTH_LONG).show();
}
}
}
}
}
};
我通过 createPendingRequest 进行 Post 调用,此响应由另一个处理程序处理,即
public Handler messageHandleralert = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch(msg.what){
case 5:
try{
HashMap<String, String> map = new HashMap<String, String>();
DateClass date_obj = new DateClass();
String date = date_obj.getDate();
map.put("date",date);
map.put("sender_email", sender);
mOutAdapter.insert(map, 0);
mprogDialog.dismiss();
String sData = date+" "+sender;
System.out.println(active);
showAlert showDialog = new showAlert(mOutAdapter,context,null,null);
AlertDialog.Builder alert = showDialog.createAlert("Received Payment Request", "Confirm Details of the Payee", true, false,true,sData);
alert.show();
}
catch(Exception e){
e.printStackTrace();
mprogDialog.dismiss();
Toast.makeText(Tab_Activity.this, "Error... Do it Again", Toast.LENGTH_LONG).show();
}
break;
case 6:
try{
Toast.makeText(Tab_Activity.this, "Error. Check Connection and Try Again", Toast.LENGTH_LONG).show();
mprogDialog.dismiss();
}
catch(Exception e){
e.printStackTrace();
}
break;
default:
break;
}
}
};
所以错误出现在我的第一个处理程序块的 ProgressDialog 行。
mprogDialog = ProgressDialog.show(Tab_Activity.this, "", "creating pending request");
这是
android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@41520070 is not valid; is your activity running?
实际上,我可以通过不显示任何进度来摆脱这条线。但是我的第二个处理程序块中的 AlertDialog 是必需的,它在活动重新启动时会引发相同的错误。
我试过包括
(!isFinishing())
但这不起作用。如果我使用它,则在活动重新启动时永远不会到达该代码。
有没有办法解决这个问题?还是我做错了什么?一些帮助甚至链接将不胜感激
日志猫:
android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@414f3fa8 is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:646)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:301)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
at android.view.Window$LocalWindowManager.addView(Window.java:537)
at android.app.Dialog.show(Dialog.java:278)
at android.app.ProgressDialog.show(ProgressDialog.java:116)
at android.app.ProgressDialog.show(ProgressDialog.java:99)
at android.app.ProgressDialog.show(ProgressDialog.java:94)
at org.example.pay.Tab_Activity$1.handleMessage(Tab_Activity.java:159)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4514)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)