繁体   English   中英

android-在片段中的runOnUiThread内部显示进度对话框

[英]android - displaying progress dialog inside runOnUiThread in fragment

我有一个简单的android应用程序,当我按“激活”按钮时,应显示进度对话框。 基本上,当我按“激活”时,该应用将在检测附近的传感器时显示进度对话框。 我还要在检测到传感器的同时更新进度对话框消息。 以下代码在该片段内实现。

但是我的代码没有显示进度对话框。 当我按下“按钮”时,我什么都看不到。 好像冻结了。

    @Override
    public void onClick( View v) {
    v.setEnabled(false);
    activateButton.setEnabled(true);
    final Activity activity = getActivity();
    final ProgressDialog pd = new ProgressDialog(activity);
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            pd.setTitle("Activating...");
            pd.setMessage("Please wait...");
            pd.setCancelable(false);
            pd.setIndeterminate(false);
            pd.show();

            int count = 1;
            pd.setMessage("Detecting sensors...\n");
            while (true) {
                if (count >= 10) {
                    break;
                }
                count++;
                try {
                    pd.setMessage("Detected " + count + "=" + " sensor. \n");
                    Thread.sleep(1000 * 1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            pd.dismiss();
        }
    });

根据评论,我更改为此。

        AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
        ProgressDialog pd;

        @Override
        protected void onPreExecute() {

            pd = new ProgressDialog(getActivity());
            pd.setTitle("Activating...");
            pd.setMessage("Please wait...");
            pd.setCancelable(false);
            pd.setIndeterminate(false);
            pd.show();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            int count = 1;
            pd.setMessage("Detecting sensors...\n");
            try {
                Thread.sleep(1000 * 1);
            } catch (InterruptedException e1) {
                //
            }
            StringBuffer txt = new StringBuffer();
            txt.append(".....");
            while (true) {
                if (count >= 10) {
                    break;
                }
                count++;
                pd.setMessage("Detected " + count + "=" + " sensor. \n");
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            if (pd != null) {
                pd.dismiss();
                activateButton.setEnabled(true);
            }
        }

    };
    task.execute((Void[]) null);

但是,当这段代码运行时,我在这一行中收到此错误(pd.setMessage(“ Detected” + count +“ =” +“ sensor。\\ n”);)。

原因:android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸其视图。

如果有人可以指出问题出在哪里,这将很有帮助。

谢谢。

您正在从后台线程更新视图。 从方法“ doInBackground”中,应该调用“ publishProgress”方法,然后在“ onProgressUpdate”中更新进度对话框(将在UI线程中执行)。 检查以下代码:

   new AsyncTask<Void, String, Void>() {
        public ProgressDialog mProgress;

        @Override
        protected void onPreExecute() {
            mProgress = ProgressDialog.show(getContext(), "Progress", "Please Wait", true);
        }

        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(1000);
                publishProgress("progress 1");
                Thread.sleep(2000);
                publishProgress("progress 2");
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            mProgress.dismiss();
        }

        @Override
        protected void onProgressUpdate(String... values) {
            mProgress.setMessage(values[0]);
        }
    };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM