我不确定我做错了什么,但onPostExecute永远不会被调用。
我读了很多类似问题的答案,但没有一个能为我工作。
我试图将返回声明替换为void和Integer以及许多对其他但不适合我的疯狂事物。
我的asyncetask课程:
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));
}
}
我从这样的主要活动中调用它:
Async async = new Async(MainActivity.this);
async.execute(from,to);
答案 0 :(得分:1)
你的问题是
switch (arr.get(0).getPoleNumber())
<{1>}中的因为onPostExecute()
NULL 。
arr
中的 doInBackground()
应为ArrayList<Result> arr = new ArrayList<Result>();
arr = new ArrayList<Result>();
应该是这样的,
doInBackground()
此外,我对 @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;
}
有疑问,因为您在Application UI线程中使用Activity上下文获取资源。不在像initializeVariables()