在我的应用程序中,我使用带有3个选项卡的tabhost,当我按下后退按钮时,我得到了WindowManager.BadTokenException
我尝试了很多东西,但我无法将其解决。
这是我的活动后台任务:
class MyTask extends AsyncTask<String, Void, String> {
ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(activity);
// pDialog = new ProgressDialog(Groupinion_MyStuff_Answer.this);
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
try {
pDialog.show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected String doInBackground(String... params) {
return Utils.getJSONString(params[0]);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (null != pDialog && pDialog.isShowing()) {
pDialog.dismiss();
}
}
}
}
这是我的Logcat
02-26 12:56:58.765: W/System.err(30960): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@42161de0 is not valid; is your activity running?
02-26 12:56:58.765: W/System.err(30960): at android.view.ViewRootImpl.setView(ViewRootImpl.java:720)
02-26 12:56:58.765: W/System.err(30960): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:356)
02-26 12:56:58.765: W/System.err(30960): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:234)
02-26 12:56:58.765: W/System.err(30960): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:153)
02-26 12:56:58.765: W/System.err(30960): at android.view.Window$LocalWindowManager.addView(Window.java:557)
02-26 12:56:58.765: W/System.err(30960): at android.app.Dialog.show(Dialog.java:277)
02-26 12:56:58.775: W/System.err(30960): at com.android.groupinion.Groupinion_MyStuff_Answer$MyTask.onPreExecute(Groupinion_MyStuff_Answer.java:269)
02-26 12:56:58.775: W/System.err(30960): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
02-26 12:56:58.775: W/System.err(30960): at android.os.AsyncTask.execute(AsyncTask.java:534)
02-26 12:56:58.775: W/System.err(30960): at com.android.groupinion.Groupinion_MyStuff_Answer.onCreate(Groupinion_MyStuff_Answer.java:182)
02-26 12:56:58.775: W/System.err(30960): at android.app.Activity.performCreate(Activity.java:5203)
02-26 12:56:58.775: W/System.err(30960): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
02-26 12:56:58.775: W/System.err(30960): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
02-26 12:56:58.775: W/System.err(30960): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1955)
02-26 12:56:58.775: W/System.err(30960): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
02-26 12:56:58.775: W/System.err(30960): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
02-26 12:56:58.775: W/System.err(30960): at com.android.groupinion.Groupinion_Mystuff.replaceContentView(Groupinion_Mystuff.java:461)
02-26 12:56:58.775: W/System.err(30960): at com.android.groupinion.Groupinion_Mystuff$3.onClick(Groupinion_Mystuff.java:202)
02-26 12:56:58.775: W/System.err(30960): at android.view.View.performClick(View.java:4191)
02-26 12:56:58.775: W/System.err(30960): at android.view.View$PerformClick.run(View.java:17229)
02-26 12:56:58.775: W/System.err(30960): at android.os.Handler.handleCallback(Handler.java:615)
02-26 12:56:58.775: W/System.err(30960): at android.os.Handler.dispatchMessage(Handler.java:92)
02-26 12:56:58.775: W/System.err(30960): at android.os.Looper.loop(Looper.java:137)
02-26 12:56:58.775: W/System.err(30960): at android.app.ActivityThread.main(ActivityThread.java:4960)
02-26 12:56:58.775: W/System.err(30960): at java.lang.reflect.Method.invokeNative(Native Method)
02-26 12:56:58.775: W/System.err(30960): at java.lang.reflect.Method.invoke(Method.java:511)
02-26 12:56:58.775: W/System.err(30960): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
02-26 12:56:58.775: W/System.err(30960): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
02-26 12:56:58.775: W/System.err(30960): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
我猜这个问题与progressDialog有关。我建议将pDialog定义为顶部活动中的对象(而不是AsyncTask的对象)
class MyActivity{
ProgressDialog pDialog;
}
在显示对话框之前,请检查活动是否已进入完成状态。
if(!activity.isFinishing)
pDialog.show();
并在活动的onDestroy方法中
protected void onDestroy(){
super.onDestroy();
if(pDialog != null && pDialog.isShowing())
pDialog.cancel();
}
答案 1 :(得分:2)
可能的原因是警报对话框的上下文。您可能已完成该活动,因此它尝试在该上下文中打开但已关闭。 尝试将该对话框的上下文更改为第一个活动,因为它将在最后完成。
例如
而不是这个。
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
尝试使用
AlertDialog alertDialog = new AlertDialog.Builder(FirstActivity.getInstance()).create();
答案 2 :(得分:1)
而不是pDialog = new ProgressDialog(activity);
,请尝试:
pDialog = new ProgressDialog(yourClass.this);
注意参数传递。