我目前有一个asyncTask
,它在preexecute开始一个加载栏,在后台发送一些东西到服务器,并在post执行后解除对话框并启用一个按钮。但是,由于doInBackground返回null,我的post执行没有执行。我想弄明白我能做什么让postExecute运行。有任何想法吗?感谢
代码:
class DatabaseAsync extends AsyncTask<Void,Void,Void>{
@Override
protected void onPreExecute(){
dialog = ProgressDialog.show(MainFeedActivity.this, null, "Posting...");
}
@Override
protected Void doInBackground(Void... arg0) {
Log.d(TAG, "send to databse");
SendToDatabase();
Log.d(TAG, "sent to database - DONE");
//dialog.dismiss();
//sendButton.setEnabled(true);
return null;
}
protected void onPostExecute(){
Log.d(TAG, "p execute");
dialog.dismiss();
sendButton.setEnabled(true);
Log.d(TAG, "done executing");
}
}
答案 0 :(得分:5)
在您的案例中,从null
返回doInBackground()
是完全可以的。只需确保onPostExecute()看起来像这样:
@Override
protected void onPostExecute(Void result) {
Log.d(TAG, "p execute");
dialog.dismiss();
sendButton.setEnabled(true);
Log.d(TAG, "done executing");
}
答案 1 :(得分:0)
像这样更改您的DatabaseAsync类:
class DatabaseAsync extends AsyncTask<String, Void, String>{
protected void onPreExecute(){
dialog = ProgressDialog.show(MainFeedActivity.this, null, "Posting...");
}
protected String doInBackground(String... arg0) {
Log.d("TAG", "send to databse");
Log.d("", "sent to database - DONE");
//dialog.dismiss();
//sendButton.setEnabled(true);
return null;
}
protected void onPostExecute(String result){
Log.d("TAG", "p execute");
dialog.dismiss();
Log.d("TAG", "done executing");
}
在代码工作http://www.vogella.de/articles/AndroidPerformance/article.html
后阅读此链接