简体   繁体   中英

AsyncTask “onPostExecute” method never called

I am not sure what I am doing wrong but onPostExecute never gets called.

I read a lot of answers to similar problem and none of them worked for me.

I tried to replace the return declaration to void and to Integer and a lot of crazy things that work for other but not for me.

my asyncetask class:

    import static java.util.Arrays.asList;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.widget.ImageView;

public class Async extends AsyncTask<Result, Void, Integer> {

            private static final String TAG = "UpdateUI";
            private static final int DELAY = 500;
            public static ImageView A1, A2, A3, A4, A5, A6, A7, A8, B1, B2, B3, B4, B5, B6,
                    B7, B8, C1, C2, C3, C4, C5, C6, C7, C8;
            public int dsc1, dsc2, dsc3, dsc4, dsc5, dsc6, dsc7, dsc8, pole;
            public static Resources mResources;
            public static List<ImageView> A ,B ,C ;
            private Activity mActivity;
            public ArrayList<Result> arr;

            Async(Activity activity) {
                mActivity = activity;   
            }

            @Override
            protected Integer  doInBackground(Result... params) {

                initializeVariables();
                mResources =  mActivity.getResources();
                ArrayList<Result> arr = new ArrayList<Result>();
                arr.add(params[0]);
                arr.add(params[1]);


                return 1;
            }

            @Override
            protected void onPostExecute(Integer result) {


                switch (arr.get(0).getPoleNumber()) {
                case 1:
                    A.get(arr.get(0).getPole()).setImageDrawable(arr.get(0).getImg());
                    break;
                case 2:
                    B.get(arr.get(0).getPole()).setImageDrawable(arr.get(0).getImg());
                    break;
                case 3:
                    C.get(arr.get(0).getPole()).setImageDrawable(arr.get(0).getImg());
                    break;
                }

                   switch (arr.get(1).getPoleNumber()) {
                    case 1:
                        A.get(arr.get(1).getPole()).setImageDrawable(arr.get(1).getImg());
                        break;
                    case 2:
                        B.get(arr.get(1).getPole()).setImageDrawable(arr.get(1).getImg());
                        break;
                    case 3:
                        C.get(arr.get(1).getPole()).setImageDrawable(arr.get(1).getImg());
                        break;
                    }

            }


            private void initializeVariables() {


                dsc1 = R.drawable.disc1;
                dsc2 = R.drawable.disc2;
                dsc3 = R.drawable.disc3;
                dsc4 = R.drawable.disc4;
                dsc5 = R.drawable.disc5;
                dsc6 = R.drawable.disc6;
                dsc7 = R.drawable.disc7;
                dsc8 = R.drawable.disc8;
                pole = R.drawable.pole;

                A1= (ImageView) mActivity.findViewById(R.id.A1);
                A2= (ImageView) mActivity.findViewById(R.id.A2);
                A3= (ImageView) mActivity.findViewById(R.id.A3);
                A4= (ImageView) mActivity.findViewById(R.id.A4);
                A5= (ImageView) mActivity.findViewById(R.id.A5);
                A6= (ImageView) mActivity.findViewById(R.id.A6);
                A7= (ImageView) mActivity.findViewById(R.id.A7);
                A8= (ImageView) mActivity.findViewById(R.id.A8);
                B1= (ImageView) mActivity.findViewById(R.id.B1);
                B2= (ImageView) mActivity.findViewById(R.id.B2);
                B3= (ImageView) mActivity.findViewById(R.id.B3);
                B4= (ImageView) mActivity.findViewById(R.id.B4);
                B5= (ImageView) mActivity.findViewById(R.id.B5);
                B6= (ImageView) mActivity.findViewById(R.id.B6);
                B7= (ImageView) mActivity.findViewById(R.id.B7);
                B8= (ImageView) mActivity.findViewById(R.id.B8);
                C1= (ImageView) mActivity.findViewById(R.id.C1);
                C2= (ImageView) mActivity.findViewById(R.id.C2);
                C3= (ImageView) mActivity.findViewById(R.id.C3);
                C4= (ImageView) mActivity.findViewById(R.id.C4);
                C5= (ImageView) mActivity.findViewById(R.id.C5);
                C6= (ImageView) mActivity.findViewById(R.id.C6);
                C7= (ImageView) mActivity.findViewById(R.id.C7);
                C8= (ImageView) mActivity.findViewById(R.id.C8);

                A = new ArrayList<ImageView>(asList(A1, A2, A3, A4, A5, A6, A7, A8));
                B = new ArrayList<ImageView>(asList(B1, B2, B3, B4, B5, B6, B7, B8));
                C = new ArrayList<ImageView>(asList(C1, C2, C3, C4, C5, C6, C7, C8));
            }
    }

I'm calling it from main activity like this:

Async async = new Async(MainActivity.this);
async.execute(from,to); 

Your problem is

switch (arr.get(0).getPoleNumber()) 

in onPostExecute() because arr is NULL .

in doInBackground() the code line ArrayList<Result> arr = new ArrayList<Result>(); should be arr = new ArrayList<Result>();

doInBackground() should be something like,

 @Override
 protected Integer  doInBackground(Result... params) {
     // doubt on this method, it should be in onPreExecute()
     initializeVariables();
     mResources =  mActivity.getResources();
     arr = new ArrayList<Result>();
     arr.add(params[0]);
     arr.add(params[1]);

     return 1;
    }

Also I have doubt over initializeVariables() as you are getting resources using Activity context it should in Application UI thread. not in worker thread like doInbackground()

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