0

我正在为包含进度条和文本视图的对话框使用自定义布局。如果对话框中的文本已经在运行,我想更改它。这是我执行此操作的代码。

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

请帮我解决这个问题。

4

2 回答 2

2

您应该在MainThreadUIThread创建视图层次结构的唯一线程可以触摸其视图。)上调用这些方法,如果您在分开的thread情况下调用它们,您应该使用方法调用它们runOnUiThread()::

YourActivity.this.runOnUiThread(new Runnable() {

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

编辑:这里是一个教程,显示了ProgressDialog从服务器下载文件的时间,我认为这正是你想要的,看看它

于 2013-03-26T10:06:50.363 回答
0

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

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

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

于 2013-03-26T10:32:50.767 回答