【发布时间】:2014-09-30 19:58:01
【问题描述】:
我在 AyncTask 的一个代码上坐了 5 个小时,但运行不正常。我刚刚创建了另一个简单的Activity(因为在最后一个onPostExecute() 没有工作)现在这个简单的Activity 也没有启动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 线程。当您尝试运行它时会发生什么?
-
这是我的[对类似问题的回答][1] [1]:stackoverflow.com/questions/25752210/…
-
嗯,我正在从 onPostExecute() 更新 UI 线程,所以可能是我的问题(它没有更新任何东西,甚至没有做任何没有被调用的事情)。那明天试试感谢您的建议。
-
你真的应该使用@Override 注释。您的编译器会立即告诉您问题。
标签: android android-asynctask navigation-drawer progressdialog