2013-03-26 55 views
0

我爲包含進度條和textview的對話框使用了自定義佈局。如果它已經在運行,我想要更改對話框中的文本。這是我做這個操作的代碼。更改運行對話框中的文本

private static Dialog progressDialog = null; 
public static void showLoadingProgress(String msg){ 
     Log.d("Util", "show loading progress"+progressDialog);//No i18n 
     if(progressDialog!=null && progressDialog.isShowing()){ 
      TextView message = (TextView)progressDialog.findViewById(R.id.progressmsg); 
      message.setText(msg);   
     }else{   
      progressDialog = new Dialog(MainActivity.getActivity().getApplicationContext()); 
      progressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
      progressDialog.setCancelable(false); 
      progressDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); 
      progressDialog.setContentView(R.layout.customprogress); 

      TextView message = (TextView)progressDialog.findViewById(R.id.progressmsg); 
      message.setText(msg); 

      progressDialog.show(); 
      Log.d("",progressDialog.isShowing()+""); //No i18n 
     } 
    } 

    public static void hideLoadingProgress(){ 
     Log.d("Util", "close loading progress"+progressDialog);//No i18n 

     if(progressDialog!=null){ 
     Log.d("ProgressDialog is not null, so making it null",""); //No i18n 
     progressDialog.dismiss(); 
     progressDialog = null; 
     } 
    } 

我從try塊中調用此方法,並從asyncTask doInBackground()中的catch塊中調用此方法。從try塊首先調用顯示對話框,但如果發生任何錯誤,再次調用從catch塊發生,引發此異常

03-26 13:42:53.002: E/AndroidRuntime(16151): java.lang.RuntimeException: An error occured while executing doInBackground() 
03-26 13:42:53.002: E/AndroidRuntime(16151): at android.os.AsyncTask$3.done(AsyncTask.java:299) 
03-26 13:42:53.002: E/AndroidRuntime(16151): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 
03-26 13:42:53.002: E/AndroidRuntime(16151): at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 
03-26 13:42:53.002: E/AndroidRuntime(16151): at java.util.concurrent.FutureTask.run(FutureTask.java:239) 
03-26 13:42:53.002: E/AndroidRuntime(16151): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
03-26 13:42:53.002: E/AndroidRuntime(16151): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
03-26 13:42:53.002: E/AndroidRuntime(16151): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
03-26 13:42:53.002: E/AndroidRuntime(16151): at java.lang.Thread.run(Thread.java:856) 
03-26 13:42:53.002: E/AndroidRuntime(16151): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
03-26 13:42:53.002: E/AndroidRuntime(16151): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746) 
03-26 13:42:53.002: E/AndroidRuntime(16151): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:823) 
03-26 13:42:53.002: E/AndroidRuntime(16151): at android.view.View.requestLayout(View.java:15468) 
03-26 13:42:53.002: E/AndroidRuntime(16151): at android.view.View.requestLayout(View.java:15468) 
03-26 13:42:53.002: E/AndroidRuntime(16151): at android.view.View.requestLayout(View.java:15468) 
03-26 13:42:53.002: E/AndroidRuntime(16151): at android.view.View.requestLayout(View.java:15468) 
03-26 13:42:53.002: E/AndroidRuntime(16151): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:318) 
03-26 13:42:53.002: E/AndroidRuntime(16151): at android.view.View.requestLayout(View.java:15468) 
03-26 13:42:53.002: E/AndroidRuntime(16151): at android.view.View.requestLayout(View.java:15468) 
03-26 13:42:53.002: E/AndroidRuntime(16151): at android.widget.TextView.checkForRelayout(TextView.java:6313) 
03-26 13:42:53.002: E/AndroidRuntime(16151): at android.widget.TextView.setText(TextView.java:3567) 
03-26 13:42:53.002: E/AndroidRuntime(16151): at android.widget.TextView.setText(TextView.java:3425) 
03-26 13:42:53.002: E/AndroidRuntime(16151): at android.widget.TextView.setText(TextView.java:3400 

請幫我解決這個問題。

回答

2

你應該呼籲MainThread這些方法(UIThread創建視圖層次可以觸摸其觀點的唯一線索),如果你打電話給他們在分離thread你應該使用方法調用它們:runOnUiThread()

YourActivity.this.runOnUiThread(new Runnable() { 

      @Override 
      public void run() { 
       // TODO Auto-generated method stub 
       showLoadingProgress("Loading data..."); 
      } 
     }); 

編輯:這裏是一個tutorial這顯示ProgressDialog同時下載從服務器上的文件,我認爲它正是你想要的,採取看看它

0

我認爲你必須「改爲」你的文字和進度對話框,通過重寫你的AsyncTask的onProgressUpdate():

@Override 
    protected void onProgressUpdate(Progress... values) { 
     // Do your change here with your own data 
    } 

doInBackground()應該在後臺執行計算線。