【问题标题】:AsyncTask: onPostExecute is not being invokedAsyncTask:onPostExecute 没有被调用
【发布时间】:2015-07-06 14:50:53
【问题描述】:

我试图在完成 doInBackground() 方法后调用 onPostExecute() 方法,然后将自定义 arrayList<ItemDTO> 传递给 Map 活动,但未调用 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;
        }
    }

}

【问题讨论】:

  • 你为什么又从onPostExecute,从一个`处理程序'调用MyAsyncTask
  • 因为我想每 30 秒更新一次地图活动中的数据?我不知道它是否正确的方法是?
  • 我可以在这里看到一些问题,包括内存泄漏。我会使用alarmManager。我也猜想你会想把它作为后台服务运行。在服务内部设置警报,并使用onPostExecute 通知警报当前任务已完成并开始另一个循环。

标签: android android-intent android-asynctask


【解决方案1】:

您实际上并没有覆盖 AsyncTask 的正确方法签名

应该是

protected void onPostExecute(List<ItemDTO> result)

通过指定 ArrayList 您正在更改方法签名。这就是为什么使用 Override 注释很有用,因为您的 ide 或编译器会告诉您方法签名不匹配。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 2019-03-31
    • 2012-12-16
    • 2015-03-28
    相关资源
    最近更新 更多