gpt4 book ai didi

java - AsyncTask "onPostExecute"方法从未被调用

转载 作者:行者123 更新时间:2023-12-01 11:44:45 26 4
gpt4 key购买 nike

我不确定我做错了什么,但 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));
}
}

我从主要 Activity 中调用它,如下所示:

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

最佳答案

你的问题是

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

onPostExecute()因为arrNULL

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

我也对 initializeVariables() 有疑问当您使用 Activity 上下文获取资源时,它应该在应用程序 UI 线程中。不在工作线程中,例如 doInbackground()

关于java - AsyncTask "onPostExecute"方法从未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29241960/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com