我正在尝试在 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);。取消注释代码行后,它工作正常,没有问题。
谢谢。