我爲包含進度條和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
請幫我解決這個問題。