Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.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 加载繁重的UI时显示ProgressDialog_Android_Android Asynctask_Progressdialog - Fatal编程技术网

Android 加载繁重的UI时显示ProgressDialog

Android 加载繁重的UI时显示ProgressDialog,android,android-asynctask,progressdialog,Android,Android Asynctask,Progressdialog,我经常使用asynctask,但是这次它不起作用! 我有一个UI,它包含一个viewpager和fragments。要填充视图,大约需要3秒钟。现在我想使用AsyncTask显示ProgressDialog,直到它完成。但是ProgressDialog没有显示 有人能告诉我答案吗?谢谢 onCreate(...){ setContentView(...) new LoadUI(MyActivity.this).execute(); } public class LoadUI ext

我经常使用
asynctask
,但是这次它不起作用! 我有一个UI,它包含一个
viewpager
fragments
。要填充视图,大约需要3秒钟。现在我想使用
AsyncTask
显示ProgressDialog,直到它完成。但是
ProgressDialog
没有显示

有人能告诉我答案吗?谢谢

onCreate(...){
   setContentView(...)
   new LoadUI(MyActivity.this).execute();
}

public class LoadUI extends AsyncTask<Void, Void, Void>{
    ProgressDialog pd;
    Context context;

    public LoadUI(Context mContext) {
        this.context = mContext;
        pd = new ProgressDialog(mContext);
        aViewPager = (ViewPager) findViewById(R.id.aPagerDay);
    }

    @Override
    protected void onPreExecute() {
        pd.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        //Create ViewPager
        //Create pagerAdapter
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        if (pd.isShowing()) {
            pd.dismiss();
        }
        super.onPostExecute(result);
    }

}
onCreate(…){
setContentView(…)
新建LoadUI(MyActivity.this).execute();
}
公共类LoadUI扩展了异步任务{
进展性帕金森病;
语境;
公共加载UI(上下文mContext){
this.context=mContext;
pd=新建进度对话框(mContext);
aViewPager=(ViewPager)findViewById(R.id.aPagerDay);
}
@凌驾
受保护的void onPreExecute(){
pd.show();
}
@凌驾
受保护的Void doInBackground(Void…参数){
//创建ViewPager
//创建pagerAdapter
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
if(pd.isShowing()){
pd.解散();
}
super.onPostExecute(结果);
}
}

您可以尝试两个选项:

使用
AsyncTask
的方法
get(长超时,时间单位)
如下:

task.get(1000, TimeUnit.MILLISECONDS);
这将使主线程最多等待1000毫秒
AsyncTask
的结果

或者,您可以在异步任务中显示进度对话框,直到它完成。看这个。基本上,异步任务运行时会显示进度对话框,完成时会隐藏进度对话框


你甚至还有第三个选择:“如果
Thread
足以满足您的需要,您可以使用其
join
方法。但是,如果任务需要很长时间,您仍然需要显示进度对话框,否则,由于主线程处于非活动状态的时间太长,您将获得异常。

问题是在
onCreate()
中GUI未准备就绪。如果我尝试在此状态下显示
对话框
,则不会显示任何内容。解决方案是将
对话框
移动到活动
onStart()


谢谢,但是你知道为什么progressdialog没有出现吗?你找到这个问题的解决方案了吗?把它移到onResum()!
@override
onStart(){
   new LoadUI(MyActivity.this).execute();
}