将具有Seri​​alizable接口的自定义ArrayList传递给活动

时间:2015-07-06 15:41:51

标签: android android-intent arraylist

我正在从MainActivity向服务器发送请求,然后我在GETLLRD课程中收到回复。

我想将ArrayList<ItemDTO>传递给我已在manifest.xml中注册的Map活动,但我收到以下错误。如何将自定义arrayList传递给Map活动并从AsyncTask类中的onPostExecute方法启动它?我尝试使用Serializable类中的ItemDTO

我感谢任何帮助。

错误:

07-06 17:06:18.039: E/AndroidRuntime(24568): FATAL EXCEPTION: AsyncTask #1
07-06 17:06:18.039: E/AndroidRuntime(24568): Process: com.bustracker, PID: 24568
07-06 17:06:18.039: E/AndroidRuntime(24568): java.lang.RuntimeException: An error occured while executing doInBackground()
07-06 17:06:18.039: E/AndroidRuntime(24568):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
07-06 17:06:18.039: E/AndroidRuntime(24568):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
07-06 17:06:18.039: E/AndroidRuntime(24568):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
07-06 17:06:18.039: E/AndroidRuntime(24568):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
07-06 17:06:18.039: E/AndroidRuntime(24568):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-06 17:06:18.039: E/AndroidRuntime(24568):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-06 17:06:18.039: E/AndroidRuntime(24568):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-06 17:06:18.039: E/AndroidRuntime(24568):    at java.lang.Thread.run(Thread.java:818)
07-06 17:06:18.039: E/AndroidRuntime(24568): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
07-06 17:06:18.039: E/AndroidRuntime(24568):    at com.bustracker.GetLLRD$MyAsyncTask.doInBackground(GetLLRD.java:62)
07-06 17:06:18.039: E/AndroidRuntime(24568):    at com.bustracker.GetLLRD$MyAsyncTask.doInBackground(GetLLRD.java:1)
07-06 17:06:18.039: E/AndroidRuntime(24568):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-06 17:06:18.039: E/AndroidRuntime(24568):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-06 17:06:18.039: E/AndroidRuntime(24568):    ... 4 more

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, ArrayList<ItemDTO>> {


        @Override
        protected ArrayList<ItemDTO> doInBackground(String... params) {

          .
          .
          .
          .

                Gson gson = new Gson();
                Type listType = new TypeToken<List<ItemDTO>>() {
                }.getType();
                ArrayList<ItemDTO> data = gson.fromJson(sb.toString(), listType);
                                for (ItemDTO itemDTO : data) {
                double latitude = itemDTO.getLatitude();
                double longitude = itemDTO.getLongitude();
                int route = itemDTO.getRoute();
                String direction = itemDTO.getDirection();
                System.out.println(latitude + ", " + longitude + ", "
                        + ", " + route + ", " + direction);

            }
          .
          .
          .
          .     


            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 && !result.isEmpty()) {
                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;
        }
    }

}

2 个答案:

答案 0 :(得分:1)

似乎列表是空的。请检查您是否从服务器获取了正确的数据:

java.lang.ArrayIndexOutOfBoundsException:length = 0;索引= 0

docs开始,在以下情况下会引发错误:

  

public class ArrayIndexOutOfBoundsException extends   IndexOutOfBoundsException

     

抛出以指示阵列已经存在   使用非法索引访问。该指数为负数或   大于或等于数组的大小。

答案 1 :(得分:0)

如果你可以调试它会更好。

发送可序列化数据:

intent.putExtra("datalist",(Serializable)yourList);

获取数据:

list = (ArrayList<POJOModel>)intent.getSerializableExtra("datalist");