2013-06-18 66 views
0

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(),這也從其他活動要求各地應用

+0

你使用的是tabhost或其他? –

+0

@SanketKachhela編號 – harshit

回答

0

試試這個:

public ProgressTask(ScanInboxActivity activity) { 
     dialog = new ProgressDialog(activity); 
     dialog.setIndeterminate(true); 
     dialog.setCancelable(false); 
    } 

    protected void onPreExecute() { 
     this.dialog.setMessage("Scan start"); 
     this.dialog.show(); 
    } 

編輯:

public class ProgressTask extends AsyncTask<String, Void, Boolean> { 

     /** progress dialog to show user that the backup is processing. */ 
     private ProgressDialog dialog; 

     public ProgressTask(MainMenuActivity activity) { 
      dialog = new ProgressDialog(activity); 
      dialog.setIndeterminate(true); 
      dialog.setCancelable(false); 
     } 

     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 
      try { 
       Thread.sleep(10000); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return true; 
     } 
    } 
+0

做到了。不起作用。 – harshit

+0

仍然有異常? – Dimmerg

+0

是的,幾乎一樣。差異是'在com.example.ScanInboxActivity $ ProgressTask。 (ScanInboxActivity.java:219)'now。即改變我呼叫的行號this.dialog.show() – harshit

-2
class asytask extends AsyncTask<String, Integer, String> 
     { 
      ProgressDialog dialog ; 
      @Override 
      protected void onPreExecute() { 
       // TODO Auto-generated method stub 
       super.onPreExecute(); 
       dialog = new ProgressDialog(getApplicationContext()); 
       dialog.show(); 
      } 
      @Override 
      protected String doInBackground(String... params) { 
       // TODO Auto-generated method stub 

//use your backgournd process 
       return null; 
      } 
      @Override 
      protected void onPostExecute(String result) { 
       // TODO Auto-generated method stub 
       super.onPostExecute(result); 
       dialog.dismiss(); 

// visible process 
      } 
     } 
+0

花費一些精力將代碼粘貼到「代碼」格式。你的代碼很龐大。 –

0

我的假設是,這些方法init()scanSms()你放在doInBackground(),利用了Context。如果是這樣,那麼這可能是原因,因爲doInBackground()不應該使用上下文。

嘗試在UI線程中調用onPreExecute()進行初始化,並且僅在doInBackground()中調用較重的東西。

+0

只有scanSms()使用上下文。我試着把init放在onExExcute()上。它不這樣工作。 – harshit

相關問題