onPostExecute not called after completion AsyncTask

前端 未结 8 1726
[愿得一人]
[愿得一人] 2020-12-10 09:56

For some reason my onPostExecute() is not called after my AsyncTask finishes.

My class decleration:

public class setWallpaper

相关标签:
8条回答
  • 2020-12-10 10:55

    Made another nasty mistake that can result in this same error. When defining the AsyncTask and calling it, I was not calling execute but was calling doInBackground

    new AsyncTask<String,Void,Void>() {
        ....
    }.doInBackground("parameter");
    

    rather than

    new AsyncTask<String,Void,Void>() {
        ....
    }.execute("parameter");
    
    0 讨论(0)
  • 2020-12-10 11:02

    I have faced the same problem. None of the above solutions worked for me. Then i figured out the problem maybe it helps someone else .

    In UI thread i call the following codes:

    public class XActivity ...{
        onCreate(){
            ....
    
            new SaveDrawingAsync(this).execute();
    
            while(true)
            {
                if(MandalaActivity.saveOperationInProgress){
                    continue;
                }
                super.onBackPressed();
                break;
            }
    
            ...
        }
    }
    

    My AsyncTask class definition :

    public class SaveAsync extends AsyncTask<Object, Void, Void> {
    
        @Override
        public Void doInBackground(Object... params) {
            saveThem(); // long running operation
            return null;
        }
    
        @Override
        public void onPostExecute(Void param) {
            XActivity.saveOperationInProgress = false;
        }
    
        @Override
        public void onPreExecute() {
           XActivity.saveOperationInProgress = true;
        }
    
    }
    

    in the above code onPostExecute is not called. It is because of an infinite loop after asynctask execution . asynctask and inifinite loop both waits eachother to finish. Thus the code stucks!

    The solution is changing the design!

    0 讨论(0)
提交回复
热议问题