2013-06-05 49 views
0

我想在Android中編寫一個小應用程序。 我在dialog.show()中出現以下錯誤。但它不是複製品,我不知道它爲什麼會發生。請幫忙。可能是 例外是清理東西,下一次 工作fine.Code片段中的應用低於在dialog.show中獲取BadTokenException()

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 
     [email protected] 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);.取消註釋代碼行後,它可以正常工作,無問題。

謝謝。

+0

也許發佈錯誤的日誌會對我們有點幫助嗎? – verybadalloc

+2

發生此異常的原因主要是由於上下文引用不正確。也許你通過創建Progress對話框的Activity上下文被垃圾收集。 – Anirudh

+0

您的上下文不正確。你使用什麼上下文創建對話框? – njzk2

回答

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(); 
} 


} 
+0

感謝您的快速回復,但我得到了BadToken異常,可能是它給我的上下文的影響,我該如何解決 – pavi

+0

我更新代碼請檢查出來.. – Suraj

+0

在我的代碼片段中,我已經在另一個名爲createProgressDialog()的方法中給出了更新的代碼,我們可以修改任何其他選項嗎? – pavi

相關問題