在运行对话框中更改文本

时间:2013-03-26 10:01:08

标签: android android-widget

我在我的对话框中使用自定义布局,其中包含progressbar和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 个答案:

答案 0 :(得分:2)

您应该在MainThread上调用这些方法(UIThread ,创建视图层次结构的唯一线程可以触及其视图。 ) ,如果您在分开的thread上拨打电话,则应使用以下方法调用它们:runOnUiThread()

YourActivity.this.runOnUiThread(new Runnable() {

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

编辑:这是从服务器下载文件时显示ProgressDialog的{​​{3}},我认为这正是您想要的,看看它< / p>

答案 1 :(得分:0)

我认为你必须通过覆盖AsyncTask的onProgressUpdate()来“更改”你的文本和进度对话框:

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

doInBackground()应该在后台线程上执行计算。