我试图在完成 doInBackground() 方法后调用 onPostExecute() 方法,然后传递自定义 arrayList<ItemDTO>
到 Map Activity ,但未调用 onPostExecute 方法。我没有在 Logcat 中得到任何输出作为它被调用的标志。我该如何解决?
我已经调试过了,可以看到ArrayList数据对象中的数据。
感谢任何帮助。
GetLLRD 类
public class GetLLRD {
Context mContext;
public void post_selected(String json, Context context) {
new MyAsyncTask().execute(json);
context = this.mContext;
}
class MyAsyncTask extends AsyncTask<String, Integer, List<ItemDTO>> {
@Override
protected List<ItemDTO> doInBackground(String... params) {
.
.
.
.
Gson gson = new Gson();
Type listType = new TypeToken<List<ItemDTO>>() {
}.getType();
ArrayList<ItemDTO> data = gson.fromJson(sb.toString(), listType);
.
.
.
.
return data;
}
protected void onPostExecute(ArrayList<ItemDTO> result) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
new MyAsyncTask().execute();
System.out.println("The method onPostExcute() in GETLLRD class was invoked again");
}
}, 1*30 * 1000);
if (result != null) {
Intent intent = new Intent(mContext, Map.class);
intent.putExtra("list",result);
mContext.startActivity(intent);
}else{
Log.e("123", "Avoiding null pointer, the dat is null in the GETLLRD class!!!");
}
}
}
}
具有内部类 ItemDTo 的 MapDataJSON 类,我需要从我从服务器获取的 JSON 字符串中获取数据。
public class MapDataJSON {
ArrayList<ItemDTO> items;
public MapDataJSON(ArrayList<ItemDTO> items) {
super();
this.items = items;
}
public ArrayList<ItemDTO> getItems() {
return items;
}
public void setItems(ArrayList<ItemDTO> items) {
this.items = items;
}
public static class ItemDTO implements Serializable {
private static final long serialVersionUID = 1L;
double latitude;
double longitude;
int route;
String direction;
public ItemDTO(double latitude, double longitude, int route,
String direction) {
super();
this.latitude = latitude;
this.longitude = longitude;
this.route = route;
this.direction = direction;
}
public double getLatitude() {
return latitude;
}
public double getLongitude() {
return longitude;
}
public int getRoute() {
return route;
}
public String getDirection() {
return direction;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public void setRoute(int route) {
this.route = route;
}
public void setDirection(String direction) {
this.direction = direction;
}
}
}
最佳答案
您实际上并没有覆盖 AsyncTask 的正确方法签名
应该是
protected void onPostExecute(List<ItemDTO> result)
通过指定 ArrayList,您将更改方法签名。这就是为什么使用 Override 注释很有用,因为您的 ide 或编译器会告诉您方法签名不匹配。
关于android - 异步任务 : onPostExecute is not being invoked,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31248968/