Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:ProgressDialog未在onCreate()中调用的AsyncTask中显示_Android_Android Asynctask_Android Progressbar - Fatal编程技术网

Android:ProgressDialog未在onCreate()中调用的AsyncTask中显示

Android:ProgressDialog未在onCreate()中调用的AsyncTask中显示,android,android-asynctask,android-progressbar,Android,Android Asynctask,Android Progressbar,我有一个AsyncTask,它将在onCreate方法中执行。但是,我的ProgressDialog没有出现。通过调试,确认正在执行AsyncTask @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_lifestyle); co

我有一个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();
        }
    }
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、活动和生活方式);
context=getApplicationContext();
newtestasync().execute();
}
私有类testAsync扩展异步任务{
私人对话;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newProgressDialog(MainActivity.this);//使用上下文进行了尝试,没有区别
pDialog.setTitle(“插入样本数据”);
pDialog.setMessage(“请稍候。此对话框将在完成后关闭”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…参数){
//TableControllerReadings TCR=新的TableControllerReadings(上下文);
//insertSampleData(getApplicationContext());
新建计时器()。计划(新建计时器任务()){
@凌驾
公开募捐{
//延迟5秒
}
}, 5000);
返回null;
}
@凌驾
受保护的void onPostExecute(void v){
pDialog.disclose();
}
}

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


所以最好调用
newtestasync().execute()onResume

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


所以最好调用
newtestasync().execute()
onResume

使用你的
pDialog.disease()编码到计时器线程中

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

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

私有类testAsync扩展异步任务{
私人对话;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newProgressDialog(MainActivity.this);//使用上下文进行了尝试,没有区别
pDialog.setTitle(“插入样本数据”);
pDialog.setMessage(“请稍候。此对话框将在完成后关闭”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…参数){
//TableControllerReadings TCR=新的TableControllerReadings(上下文);
//insertSampleData(getApplicationContext());
新建计时器()。计划(新建计时器任务()){
@凌驾
公开募捐{
//延迟5秒
pDialog.disclose();
}
}, 5000);
返回null;
}
@凌驾
受保护的void onPostExecute(void v){
//pDialog.disclose();
}
}

拿起你的
pDialog.disclose()编码到计时器线程中

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

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

私有类testAsync扩展异步任务{
私人对话;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newProgressDialog(MainActivity.this);//使用上下文进行了尝试,没有区别
pDialog.setTitle(“插入样本数据”);
pDialog.setMessage(“请稍候。此对话框将在完成后关闭”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…参数){
//TableControllerReadings TCR=新的TableControllerReadings(上下文);
//insertSampleData(getApplicationContext());
新建计时器()。计划(新建计时器任务()){
@凌驾
公开募捐{
//延迟5秒
pDialog.disclose();
}
}, 5000);
返回null;
}
@凌驾
受保护的void onPostExecute(void v){
//pDialog.disclose();
}
}

添加
pDialog.show()在doInBackground中添加
newtestasync().execute()在onResume中添加
pDialog.show()在doInBackground中添加
newtestasync().execute()在onResume中

您的代码与线程配合良好。睡眠(5000);如果要添加延迟,请使用Thread.Sleep(/time in milisec/)而不是Timer.Schedule。由于计时器的原因。Schedule异步任务在显示任何对话框之前执行得非常快。

您的代码在Thread.sleep(5000)上运行良好;如果要添加延迟,请使用Thread.Sleep(/time in milisec/)而不是Timer.Schedule。由于计时器的原因。Schedule async任务在显示任何对话框之前执行得非常快。

必须在UI线程中调用
.show()
方法。在
doInBackground
中校准它会让我
 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();
        }
    }