在dialog.show()中获取BadTokenException

时间:2013-06-05 12:28:05

标签: android

我正在尝试在Android中编写一个小应用程序。 我在dialog.show()中收到以下错误。但这不是复制。我不知道为什么会发生这种情况。请帮忙。可能是 例外是清理一些东西,下次申请时 工作正常。代码片段在

之下
private ProgressDialog pd = null;

public AsyncTaskAllCurrency(Context ctx,
        IAsyncTaskCompletedListner<CurrencyReturnPojo> listner) {
    this.context = ctx;
    this.callback = listner;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    pd = createProgressDialog(context, "Loading Please wait...");
    pd.show();
}

@Override
protected void onPostExecute(CurrencyReturnPojo result) {
    super.onPostExecute(result);
    callback.onTaskComplete(result);
    pd.dismiss();
}

private ProgressDialog createProgressDialog(Context context, String msg) {
    ProgressDialog progressDialog = new ProgressDialog(context);
    progressDialog.setMessage(msg);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    // progressDialog.setCancelable(false);
    return progressDialog;
}

这个例外我得到了

    android.view.WindowManager$BadTokenException: Unable to add window -- token 
        android.os.BinderProxy@41c3bda0 is not valid; is your activity running?
  at android.view.ViewRootImpl.setView(ViewRootImpl.java:567)
  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
  at android.app.Dialog.show(Dialog.java:281)
 com. 
 zyksa.converter.pro.AsyncTaskSingleCurrency.onPreExecute(AsyncTaskSingleCurrency.java:
  42)
  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
   at android.os.AsyncTask.execute(AsyncTask.java:534)
            com.
zyksa.converter.pro.GeneralConverterActivity.singleCurrencyAsynctaskCaller(GeneralConve
 rterActivity.java:1316)
   at 
 com.
zyksa.
converter.pro.GeneralConverterActivity.access$17(GeneralConverterActivity.java:1299) 
com.zyksa.converter.
 pro.GeneralConverterActivity
$FetchAllCurrencyTaskCompletedListner.onTaskComplete
(GeneralConverterActivity.java:1339)
at com.zyksa.converter.
pro.GeneralConverterActivity$
FetchAllCurrencyTaskCompletedListner.onTaskComplete(GeneralConverterActivity.java:1)
at com.zyksa.
converter.pro.AsyncTaskAllCurrency.onPostExecute(AsyncTaskAllCurrency.java:40)
  at com.zyksa.converter
 .pro.AsyncTaskAllCurrency.onPostExecute(AsyncTaskAllCurrency.java:1)
   at android.os.AsyncTask.finish(AsyncTask.java:631)
  at android.os.AsyncTask.access$600(AsyncTask.java:177)
  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:137)
  at android.app.ActivityThread.main(ActivityThread.java:5041)
  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:793)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  at dalvik.system.NativeStart.main(Native Method)

我终于找到了我的问题。不幸的是,我评论了一行代码,即progressDialog.setCancelable(false);.在取消注释代码行之后,它可以解决问题。

感谢。

1 个答案:

答案 0 :(得分:0)

试试这个:

private ProgressDialog pd = null;

public AsyncTaskAllCurrency(Context ctx,
        IAsyncTaskCompletedListner<CurrencyReturnPojo> listner) {
    this.context = ctx;
    this.callback = listner;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    pd = new ProgressDialog(ctx);
    pd.setMessage("Loading Please Wait..");
    pd.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
    pd.show();
}

@Override
protected void onPostExecute(CurrencyReturnPojo result) {
    super.onPostExecute(result);
    callback.onTaskComplete(result);
    pd.dismiss();
}


}