2012-04-10 37 views
0

代碼:進度條旋轉無限期

button.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     // fileReader(); 

     new xyz().execute(); 

    }// button 
});// button end   

private class xyz extends AsyncTask<Void, Void, Void> { 

    private final ProgressDialog dialog = new ProgressDialog(CCActivity3.this); 

    protected void onPreExecute() { 
     this.dialog.setCancelable(true); 
     this.dialog.setMessage("Please Wait..."); 
     this.dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
     this.dialog.setProgress(0); 
     this.dialog.setMax(100); 
     this.dialog.show(); 
    } 

    // put your code which preload with processDialog 
    private boolean running = true; 

    @Override 
    protected Void doInBackground(Void... params) { 

     // while(running) { 
     fileReader(); //i keep getting error here 
     //} 

     return null; 
    } 

    @Override 
    protected void onPostExecute(final Void unused) { 
     if (this.dialog.isShowing()) { 
      this.dialog.dismiss(); 
     } 
     super.onPostExecute(unused); 
     //this.dialog.dismiss(); 
    } 
} 

堆棧跟蹤:

04-10 21:05:05.398: D/skia(30039): purging 270K from font cache [38 entries] 
04-10 21:05:05.508: D/dalvikvm(30039): GC_EXPLICIT freed 1828K, 18% free 12468K/15107K, external 7017K/7937K, paused 106ms 
04-10 21:06:36.318: D/skia(30039): purging 13K from font cache [2 entries] 
04-10 21:06:36.418: D/dalvikvm(30039): GC_EXPLICIT freed 3271K, 39% free 9233K/15107K, external 7017K/7937K, paused 93ms 
04-10 21:07:37.208: W/dalvikvm(30039): threadid=9: thread exiting with uncaught exception (group=0x40015568) 
04-10 21:07:37.228: E/AndroidRuntime(30039): FATAL EXCEPTION: AsyncTask #1 
04-10 21:07:37.228: E/AndroidRuntime(30039): java.lang.RuntimeException: An error occured while executing doInBackground() 
04-10 21:07:37.228: E/AndroidRuntime(30039): at android.os.AsyncTask$3.done(AsyncTask.java:200) 
04-10 21:07:37.228: E/AndroidRuntime(30039): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274) 
04-10 21:07:37.228: E/AndroidRuntime(30039): at java.util.concurrent.FutureTask.setException(FutureTask.java:125) 
04-10 21:07:37.228: E/AndroidRuntime(30039): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308) 
04-10 21:07:37.228: E/AndroidRuntime(30039): at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
04-10 21:07:37.228: E/AndroidRuntime(30039): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 
04-10 21:07:37.228: E/AndroidRuntime(30039): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 
04-10 21:07:37.228: E/AndroidRuntime(30039): at java.lang.Thread.run(Thread.java:1019) 
04-10 21:07:37.228: E/AndroidRuntime(30039): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
04-10 21:07:37.228: E/AndroidRuntime(30039): at android.view.ViewRoot.checkThread(ViewRoot.java:2954) 
04-10 21:07:37.228: E/AndroidRuntime(30039): at android.view.ViewRoot.invalidateChild(ViewRoot.java:644) 
04-10 21:07:37.228: E/AndroidRuntime(30039): at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:670) 
04-10 21:07:37.228: E/AndroidRuntime(30039): at android.view.ViewGroup.invalidateChild(ViewGroup.java:2533) 
04-10 21:07:37.228: E/AndroidRuntime(30039): at android.view.View.invalidate(View.java:5288) 
04-10 21:07:37.228: E/AndroidRuntime(30039): at android.widget.TextView.checkForRelayout(TextView.java:5663) 
04-10 21:07:37.228: E/AndroidRuntime(30039): at android.widget.TextView.setText(TextView.java:2814) 
04-10 21:07:37.228: E/AndroidRuntime(30039): at android.widget.TextView.setText(TextView.java:2682) 
04-10 21:07:37.228: E/AndroidRuntime(30039): at android.widget.TextView.setText(TextView.java:2657) 
04-10 21:07:37.228: E/AndroidRuntime(30039): at dev.android.CCActivity3.fileReader(CCActivity3.java:207) 
04-10 21:07:37.228: E/AndroidRuntime(30039): at dev.android.CCActivity3$xyz.doInBackground(CCActivity3.java:110) 
04-10 21:07:37.228: E/AndroidRuntime(30039): at dev.android.CCActivity3$xyz.doInBackground(CCActivity3.java:1) 
04-10 21:07:37.228: E/AndroidRuntime(30039): at android.os.AsyncTask$2.call(AsyncTask.java:185) 
04-10 21:07:37.228: E/AndroidRuntime(30039): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 
04-10 21:07:37.228: E/AndroidRuntime(30039): ... 4 more 
04-10 21:07:37.258: I/Process(30039): Sending signal. PID: 30039 SIG: 9 
04-10 21:14:03.819: D/dalvikvm(30687): GC_EXTERNAL_ALLOC freed 109K, 64% free 487K/1347K, external 0K/0K, paused 4ms 
04-10 21:14:03.988: D/dalvikvm(30687): GC_EXTERNAL_ALLOC freed 4K, 64% free 491K/1347K, external 1K/513K, paused 14ms 
04-10 21:14:04.778: D/dalvikvm(30687): GC_EXTERNAL_ALLOC freed 10K, 61% free 534K/1347K, external 1521K/2013K, paused 12ms 
04-10 21:14:04.968: D/dalvikvm(30687): GC_EXTERNAL_ALLOC freed 2K, 61% free 533K/1347K, external 4856K/6064K, paused 10ms 
04-10 21:14:27.868: W/IInputConnectionWrapper(30687): getExtractedText on inactive InputConnection 
04-10 21:14:27.888: W/IInputConnectionWrapper(30687): getCursorCapsMode on inactive InputConnection 
04-10 21:14:27.888: W/IInputConnectionWrapper(30687): getExtractedText on inactive InputConnection 
04-10 21:14:27.898: W/IInputConnectionWrapper(30687): performPrivateCommand on inactive InputConnection 
04-10 21:14:38.208: D/szipinf(30687): Initializing inflate state 
04-10 21:14:38.238: D/szipinf(30687): Initializing zlib to inflate 
04-10 21:14:40.628: D/dalvikvm(30687): GC_FOR_MALLOC freed 412K, 24% free 3798K/4995K, external 7079K/7937K, paused 27ms 
04-10 21:14:40.808: D/dalvikvm(30687): GC_CONCURRENT freed 405K, 18% free 5440K/6595K, external 7079K/7937K, paused 0ms+4ms 
04-10 21:14:41.038: D/dalvikvm(30687): GC_CONCURRENT freed 7K, 10% free 7457K/8259K, external 7079K/7937K, paused 1ms+4ms 
04-10 21:14:41.288: D/dalvikvm(30687): GC_CONCURRENT freed 351K, 11% free 9202K/10307K, external 7079K/7937K, paused 1ms+7ms 
04-10 21:14:41.488: D/dalvikvm(30687): GC_FOR_MALLOC freed 6K, 7% free 10789K/11587K, external 7079K/7937K, paused 75ms 
04-10 21:14:41.758: D/dalvikvm(30687): GC_CONCURRENT freed 522K, 10% free 12363K/13635K, external 7079K/7937K, paused 0ms+9ms 
04-10 21:14:42.118: D/dalvikvm(30687): GC_CONCURRENT freed 8K, 6% free 14316K/15107K, external 7079K/7937K, paused 1ms+9ms 
04-10 21:15:55.768: D/skia(30687): purging 340K from font cache [47 entries] 
04-10 21:15:55.888: D/dalvikvm(30687): GC_EXPLICIT freed 1817K, 17% free 12542K/15107K, external 7079K/7937K, paused 107ms 
04-10 21:16:42.368: D/skia(30687): purging 13K from font cache [2 entries] 
04-10 21:16:42.478: D/dalvikvm(30687): GC_EXPLICIT freed 1038K, 24% free 11524K/15107K, external 7079K/7937K, paused 102ms 
04-10 21:18:28.688: D/skia(30687): purging 13K from font cache [2 entries] 
04-10 21:18:28.788: D/dalvikvm(30687): GC_EXPLICIT freed 6921K, 70% free 4646K/15107K, external 7079K/7937K, paused 91ms 
04-10 21:18:33.758: W/dalvikvm(30687): threadid=9: thread exiting with uncaught exception (group=0x40015568) 
04-10 21:18:33.768: E/AndroidRuntime(30687): FATAL EXCEPTION: AsyncTask #1 
04-10 21:18:33.768: E/AndroidRuntime(30687): java.lang.RuntimeException: An error occured while executing doInBackground() 
04-10 21:18:33.768: E/AndroidRuntime(30687): at android.os.AsyncTask$3.done(AsyncTask.java:200) 
04-10 21:18:33.768: E/AndroidRuntime(30687): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274) 
04-10 21:18:33.768: E/AndroidRuntime(30687): at java.util.concurrent.FutureTask.setException(FutureTask.java:125) 
04-10 21:18:33.768: E/AndroidRuntime(30687): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308) 
04-10 21:18:33.768: E/AndroidRuntime(30687): at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
04-10 21:18:33.768: E/AndroidRuntime(30687): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 
04-10 21:18:33.768: E/AndroidRuntime(30687): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 
04-10 21:18:33.768: E/AndroidRuntime(30687): at java.lang.Thread.run(Thread.java:1019) 
04-10 21:18:33.768: E/AndroidRuntime(30687): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
04-10 21:18:33.768: E/AndroidRuntime(30687): at android.view.ViewRoot.checkThread(ViewRoot.java:2954) 
04-10 21:18:33.768: E/AndroidRuntime(30687): at android.view.ViewRoot.invalidateChild(ViewRoot.java:644) 
04-10 21:18:33.768: E/AndroidRuntime(30687): at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:670) 
04-10 21:18:33.768: E/AndroidRuntime(30687): at android.view.ViewGroup.invalidateChild(ViewGroup.java:2533) 
04-10 21:18:33.768: E/AndroidRuntime(30687): at android.view.View.invalidate(View.java:5288) 
04-10 21:18:33.768: E/AndroidRuntime(30687): at android.widget.TextView.checkForRelayout(TextView.java:5663) 
04-10 21:18:33.768: E/AndroidRuntime(30687): at android.widget.TextView.setText(TextView.java:2814) 
04-10 21:18:33.768: E/AndroidRuntime(30687): at android.widget.TextView.setText(TextView.java:2682) 
04-10 21:18:33.768: E/AndroidRuntime(30687): at android.widget.TextView.setText(TextView.java:2657) 
04-10 21:18:33.768: E/AndroidRuntime(30687): at dev.android.CCActivity3.fileReader(CCActivity3.java:207) 
04-10 21:18:33.768: E/AndroidRuntime(30687): at dev.android.CCActivity3$xyz.doInBackground(CCActivity3.java:110)//file reader 
04-10 21:18:33.768: E/AndroidRuntime(30687): at dev.android.CCActivity3$xyz.doInBackground(CCActivity3.java:1) 
04-10 21:18:33.768: E/AndroidRuntime(30687): at android.os.AsyncTask$2.call(AsyncTask.java:185) 
04-10 21:18:33.768: E/AndroidRuntime(30687): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 
04-10 21:18:33.768: E/AndroidRuntime(30687): ... 4 more 

我的App有一點滯後,所以我決定把在進度條微調,而應用程序加載。我的代碼在進度條旋轉約6分鐘後顯示錯誤。它在fileReader()上拋出一個錯誤,有時它會永遠旋轉。 FileReader()是另一個類文件,它是代碼的核心。如果你希望看到這個課程,請點擊here 請幫助我。我很感激。

+0

請發佈您收到錯誤的堆棧跟蹤。 – elevine 2012-04-10 23:20:12

+0

我已經把錯誤,你可以向下滾動檢查。謝謝 – ImGeorge 2012-04-11 01:32:50

回答

2

問題是您正試圖從doInBackground方法更改UI。根本原因可能是您的FileReader班級在做display.setText(fg);。當你在doInBackground不能更改Acticity's用戶界面。您需要使用AsyncTask的publishProgressonProgressUpdate方法來完成此操作。

+0

可以給我一個關於如何使用onprogressupdate的想法。我是否將fileReader()放在onprogressupdate類中。謝謝 – ImGeorge 2012-04-11 05:31:15

+0

AsyncTask Javadoc頂部有一個非常簡單的例子:http://developer.android.com/reference/android/os/AsyncTask.html。您將不得不分解fileReader,以便在doInBackground中完成工作,並在onProgressUpdate中完成UI更新。 – elevine 2012-04-11 13:56:02

+0

非常感謝你現在的工作。與Java桌面應用程序相比,Android Gui很困難。 – ImGeorge 2012-04-12 00:04:08

0

this.dialog.dismiss();可以從if語句中刪除,只要運行onPostExecute時,該對話框就會自動關閉。

編輯:我的代碼的建議:

在類啓動的AsyncTask:

  ProgressDialog progressDialog = new ProgressDialog(getActivity()); 
      progressDialog.setMessage("Logging in..."); 
      LoginTask loginTask = new LoginTask((Polling) getActivity(), progressDialog); 
      loginTask.execute(); 

而且的AsyncTask本身:

public class LoginTask extends AsyncTask<String, Void, Integer> { 

private ProgressDialog progressDialog; 
private Polling activity; 
public LoginTask(Polling activity, ProgressDialog progressDialog) 
{ 
    this.activity = activity; 
    this.progressDialog = progressDialog; 
} 

@Override 
protected void onPreExecute() 
{ 
    progressDialog.show(); 
} 

protected Integer doInBackground(String... arg0) { 
    EditText userName = (EditText)activity.findViewById(R.id.emailEditText); 
    EditText passwordEdit = (EditText)activity.findViewById(R.id.passEditText); 
    String email = userName.getText().toString(); 
    String password = passwordEdit.getText().toString(); 
    UserFunctions userFunction = new UserFunctions(); 
    JSONObject json = userFunction.loginUser(email, password); 

    // check for login response 
    try { 
     if (json.getString(KEY_SUCCESS) != null) { 
      String res = json.getString(KEY_SUCCESS); 

      if(Integer.parseInt(res) == 1){ 
       //whole bunch of extra code! 
       responseCode = 1; 
       // Close Login Screen 
       //finish(); 

      }else{ 
       responseCode = 0; 
       // Error in login 
      } 
     } 

    } catch (NullPointerException e) { 
     e.printStackTrace(); 

    } 
    catch (JSONException e) { 
     e.printStackTrace(); 
    } 

    return responseCode; 
} 

@Override 
protected void onPostExecute(Integer responseCode) 
{ 
    EditText userName = (EditText)activity.findViewById(R.id.emailEditText); 
    EditText passwordEdit = (EditText)activity.findViewById(R.id.passEditText); 

    if (responseCode == 1) { 
     progressDialog.dismiss(); 
     activity.loginReport(responseCode); 
     userName.setText(""); 
     passwordEdit.setText(""); 
     //shared prefences, store name 

    } 
    if (responseCode == 0) { 
     progressDialog.dismiss(); 
     activity.loginReport(responseCode); 

    } 
} 

}

與只需按照沿progressDialog。它在啓動任務的主類中啓動,並傳遞給AsyncTask。然後,當AsyncTask構造時,progressDialog將作爲一個字段存儲在AsyncTask中。然後,在preExecute中,我們顯示()progressDialog。然後,根據doInBackground的結果,我們發送1或0給onPostExecute。那一個或零告訴onPostExecute要做什麼,在我的代碼中,哪種方式會消除對話框,但是然後在主類中運行不同的方法。希望這可以幫助你將相似的任務與你自己的任務進行比較

+0

super.onPostExecute(未使用); this.dialog.dismiss(); 我是這樣做的。但它仍然像永遠旋轉 – ImGeorge 2012-04-11 01:18:17

+0

嗯,看看我的編輯,並將您的代碼與我自己的代碼進行比較,這可能會有所幫助。 – Davek804 2012-04-11 01:38:55

0
runOnUiThread(new Runnable() { 
@Override 
public void run() { 
// TODO Auto-generated method stub 
if (this.dialog.isShowing()) { 
this.dialog.dismiss(); 
} 
}); 

在doInBackground()的catch塊中使用上面的代碼。

+0

dialog.dismiss在上面的代碼中已經在ui線程中被onPostExecute調用。我不明白這會有什麼幫助。 – elevine 2012-04-11 02:44:47