简体   繁体   中英

Can't update UI from AsyncTask

i'm tryng to change some UI elements from an n AsyncTask' onPostExecute() method but it doesn't work...i don't know why I've done it a few times earlier and it worked without any problem....the task uploads a photo to a server, it works; i don't care of results for now just want to write something on the UI

mainactivity code:

 ImageUpload post=new ImageUpload(imagePath.toString(),textView);
               //post.delegate=this;
               post.execute(imagePath.toString());

asyncTask code:

public class ImageUpload extends AsyncTask<String, Void, Void>{

    public TextView resp;

    public ImageUpload(String photoPath, TextView resp){
        //other stuff
        this.resp=resp;
    }

    @Override
    protected Void doInBackground(String... params) {
         //upload photo
    }

    protected void onPostExecute(Void... params) {
        resp.setText("work done");
    }
}

the file is uploaded but the textview doesn't change.... anyone can see the error?

You have to write the onPostExecute like this

@Override
protected void onPostExecute(Void params) {
    resp.setText("work done");
}

Here, you miss 2 things... 1. to return null in doInBackground method 2. to add @Override notation before onPostExecute method

@Override
protected Void doInBackground(String... params) {
    //upload photo
    return null; //you have to return a null for Void
}

@Override
protected void onPostExecute(Void result) {
    resp.setText("work done");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM