Android - on post execute in AsyncTask

北城余情 提交于 2019-12-23 19:53:31

问题


I currently have an asyncTask which on preexecute begins a loading bar, in background send something to a server, and on post execute dismisses the dialog and enables a button. However, my post execute is not executing due to doInBackground returning null. I'm trying to figure out what I can do do get the postExecute to run. any ideas? thanks

code:

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");
        }

    }

回答1:


It is completely Ok to return null from doInBackground() in your case. Just make sure onPostExecute() looks like this:

@Override
protected void onPostExecute(Void result) {
    Log.d(TAG, "p execute");
    dialog.dismiss();
    sendButton.setEnabled(true);
    Log.d(TAG, "done executing");
}



回答2:


change your DatabaseAsync class like this:

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");
        }

read this link after the code works http://www.vogella.de/articles/AndroidPerformance/article.html



来源:https://stackoverflow.com/questions/8369539/android-on-post-execute-in-asynctask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!