我坐在一个代码上的5小时,其中AyncTask
运行不正常。我刚刚创建了另一个简单的Activity
(因为在最后一个onPostExecute()
不起作用),现在这个简单的Activity
也不会启动AsyncTask
。任何人都可以看到我做错了什么?AsyncTask甚至没有启动
public class ServerStatus extends Activity {
Context context;
private ProgressDialog pd;
int a;
TextView test;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.server_status);
context=this;
test=(TextView) findViewById(R.id.welcomemessage);
new Download().execute();
}
public class Download extends AsyncTask<Void, Void, Void>{
protected Void onPreExecute(Void... arg0) {
pd = new ProgressDialog(context);
pd.setTitle("Processing...");
pd.setMessage("Please wait.");
pd.setCancelable(false);
pd.setIndeterminate(true);
pd.show();
return null;
}
@Override
protected Void doInBackground(Void... arg0) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
a++;
return null;
}
protected Void onPostExecute(Void... arg0) {
if (pd!=null)
pd.dismiss();
test.setText(a);
return null;
}
}
}
另外,是否NavigationDrawer
阻止UI线程?因为我实施它时甚至不能更新TextView
。
您应该在onProgressUpdate()内做UI组件修改。导航抽屉不会阻塞UI线程。当你尝试并运行它时会发生什么? – 2014-09-30 20:10:09
这里是我的[回答类似的问题] [1] [1]:http://stackoverflow.com/questions/25752210/cant-see-progressdialog-while-asynctask-run-in-background/25752553#25752553 – user3144836 2014-09-30 20:14:37
那么,我是从onPostExecute()更新UI线程,所以也许有我的问题(它没有更新任何东西,甚至没有做任何事情,就像它没有被调用)。那么明天再试试看吧。感谢您的建议。 – Darknez 2014-09-30 20:15:03