AIM我有一個列表視圖和幾個按鈕和一些額外的東西的活動。我想要的是在活動開始時使用自定義適配器填充此列表視圖。由於生成此自定義適配器需要時間,因此我想添加一個進度條,以便用戶不必被迫盯着無響應的屏幕。完成後,關閉此進度條並按照應有的方式顯示活動。無法添加窗口異常,同時顯示進度條
我做了什麼 - 「CODE」
public void onCreate(Bundle savedInstanceState) {
...
// all initialization done here properly
new ProgressTask(this).execute("");
}
public class ProgressTask extends AsyncTask<String, Void, Boolean> {
public ProgressTask(ScanInboxActivity activity) {
dialog = new ProgressDialog(activity.getApplicationContext());
dialog.setIndeterminate(true);
dialog.setCancelable(false);
}
/** progress dialog to show user that the backup is processing. */
private ProgressDialog dialog;
protected void onPreExecute() {
this.dialog.setMessage("Scan start");
this.dialog.show();
}
@Override
protected void onPostExecute(final Boolean success) {
if (dialog.isShowing()) {
dialog.dismiss();
}
populateList();
}
protected Boolean doInBackground(final String... args) {
init(); // tasks to be performed- no error here
scanSms(); // tasks to be performed- no error here
return true;
}
}
我獲得以下異常:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.ScanInboxActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRoot.setView(ViewRoot.java:509)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.app.Dialog.show(Dialog.java:241)
at com.example.ScanInboxActivity$ProgressTask.<init>(ScanInboxActivity.java:212)
at com.example.ScanInboxActivity.onCreate(ScanInboxActivity.java:59)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
... 11 more
請注意,當我寫這篇文章
public void onCreate(Bundle savedInstanceState) {
// all initialization done here properly
init();
scanSms();
populateList();
}
工作的事情很好,雖然啓動此活動的活動暫時停止。我不希望發生這種情況。
請建議該怎麼做。提前致謝。
編輯 scanSms()使用上下文。因爲它是在一個函數中使用(在不同的類),我不能消除使用上下文的堪稱scanSms(),這也從其他活動要求各地應用
你使用的是tabhost或其他? –
@SanketKachhela編號 – harshit