【问题标题】:Android: ProgressDialog not showing in AsyncTask called in onCreate()Android:ProgressDialog 未显示在调用 onCreate() 的 AsyncTask 中
【发布时间】:2016-12-18 00:16:52
【问题描述】:

我有一个 AsyncTask,它将在 onCreate 方法中执行。但是,我的 ProgressDialog 没有出现。并且通过调试确认AsyncTask正在执行。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lifestyle);
        context = getApplicationContext();

        new testAsync().execute();
}

private class testAsync extends AsyncTask<Void,Void,Void> {
        private ProgressDialog pDialog;

        @Override
        protected  void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this); // tried with context, no difference
            pDialog.setTitle("Inserting sample data");
            pDialog.setMessage("Please wait. This dialog will be dismissed upon completion.");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
          //  TableControllerReadings TCR = new TableControllerReadings(context);
          //  TCR.insertSampleData(getApplicationContext());

            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                 //delay for 5 seconds
                }
            }, 5000);

            return null;
        }

        @Override
        protected void onPostExecute(Void v) {
            pDialog.dismiss();
        }
    }

【问题讨论】:

    标签: android android-asynctask android-progressbar


    【解决方案1】:

    将您的 pDialog.dismiss(); 带入您的计时器线程。

    原因: onPostExecute() 立即调用,因为后台任务已完成。

    它的单独线程处于延迟状态,因此光标移动到onPostExecute()

     private class testAsync extends AsyncTask<Void, Void, Void> {
            private ProgressDialog pDialog;
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(MainActivity.this); // tried with context, no difference
                pDialog.setTitle("Inserting sample data");
                pDialog.setMessage("Please wait. This dialog will be dismissed upon completion.");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(false);
                pDialog.show();
            }
    
            @Override
            protected Void doInBackground(Void... params) {
                //  TableControllerReadings TCR = new TableControllerReadings(context);
                //  TCR.insertSampleData(getApplicationContext());
    
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        //delay for 5 seconds
                        pDialog.dismiss();
                    }
                }, 5000);
    
                return null;
            }
    
            @Override
            protected void onPostExecute(Void v) {
             // pDialog.dismiss();
            }
        }
    

    【讨论】:

    • 好的,这很好用。不知道哪里出了问题,但谢谢!
    【解决方案2】:

    在调用onResume 方法之前,Activity 不会显示任何视图。如果您的AsyncTask 执行在onResume 调用之前完成,那么您将永远不会看到ProgressDialog

    所以最好打电话给new testAsync().execute();onResume

    【讨论】:

    • 不强制调用 onresume 或 oncreate 来决定何时显示的逻辑或设计。
    • 我会在onResume中执行异步任务时修改我的逻辑。我收回我之前的评论,显然它现在有效。
    【解决方案3】:

    在 doInBackground 中添加 pDialog.show(); 并在 onResume 中添加 new testAsync().execute();

    【讨论】:

    • .show() 方法必须在 UI 线程中调用。在doInBackground 中调用它会给我“无法在未调用 Looper.prepare() 的线程内创建处理程序”。
    【解决方案4】:

    您的代码与 Thread.sleep(5000); 一起工作正常;如果您想添加延迟,请使用 Thread.Sleep(/time in milisec/) 而不是 Timer.Schedule。由于 Timer.Schedule 异步任务在显示任何对话框之前执行得如此之快。

    【讨论】:

      猜你喜欢
      • 2012-08-22
      • 1970-01-01
      • 2011-02-17
      • 1970-01-01
      • 1970-01-01
      • 2014-10-11
      • 2016-12-19
      • 2012-08-13
      相关资源
      最近更新 更多