调用MainActivity时我正在加载效果。我不知道为什么我的Dialog.show不能在AsyncTask中工作。所有我只是看到它解散的瞬间,但对话从未出现过。 谢谢。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new LoadViewTask().execute();
setContentView(R.layout.activity_main);
....}
private class LoadViewTask extends AsyncTask<Void, Integer, Void>
{
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(MainActivity.this,"Loading...","Loading application View, please wait...", false, false);
}
@Override
protected Void doInBackground(Void... params)
{
try
{
synchronized (this)
{
int counter = 0;
while(counter <= 4)
{
this.wait(1000);
counter++;
publishProgress(counter*25);
}
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values)
{
progressDialog.setProgress(values[0]);
}
@Override
protected void onPostExecute(Void result)
{
progressDialog.dismiss();
}
}
答案 0 :(得分:0)
您应该使用FragmentDialogs在Android中使用对话框。
这里有很好的解释:
http://developer.android.com/intl/es/reference/android/app/DialogFragment.html
答案 1 :(得分:0)
尝试在启动异步任务之前设置内容视图:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content view first
setContentView(R.layout.activity_main);
new LoadViewTask().execute();
....}