将ArrayList传递给使用RecyclerView的getData

时间:2015-06-01 19:06:16

标签: java android arraylist

我想将一个ArrayList传递给我的getData函数以供RecyclerView使用。 我有 TrainingDay 类,有ArrayList<TrainingExercise> exercises字段。我使用ArrayList值填充此JSON,如下所示:

(...)
for (int i = 0; i < trainingDays.length(); i++) {
     TrainingDay trainingDay = new TrainingDay();
     ArrayList<TrainingExercise> trainingExerciseArrayList = new ArrayList<>();

     JSONObject trainingDaysJSONObject = trainingDays.getJSONObject(i);
     JSONArray exercies = trainingDaysJSONObject.getJSONArray("exercises");

     trainingDay.setTrainingDayID(trainingDaysJSONObject.getInt("trainingDayID"));
     trainingDay.setDayNr(trainingDaysJSONObject.getInt("dayNr"));
     trainingDay.setTrainingPlanID(trainingDaysJSONObject.getInt("trainingPlanID"));



     for (int j = 0; j < exercies.length(); j++) {
         TrainingExercise trainingExercise = new TrainingExercise();

         JSONObject exerciesJSONObject = exercies.getJSONObject(j);

         trainingExercise.setExercise_id(exerciesJSONObject.getInt("exercise_id"));
         trainingExercise.setName(exerciesJSONObject.getString("name"));
         trainingExercise.setDesription(exerciesJSONObject.getString("description"));
         trainingExercise.setUrl(exerciesJSONObject.getString("url"));
         trainingExercise.setPermission(exerciesJSONObject.getInt("permission"));
         trainingExercise.setUser_id(exerciesJSONObject.getInt("user_id"));
         trainingExercise.setRepeats(exerciesJSONObject.getInt("repeats"));
         trainingExercise.setSeries(exerciesJSONObject.getInt("series"));
         trainingExercise.setTrainingDayID(exerciesJSONObject.getInt("trainingDayID"));

         trainingExerciseArrayList.add(trainingExercise);
     }
     trainingDay.setExercises(trainingExerciseArrayList);
     trainingDayArrayList.add(trainingDay);
(...)

我知道我的trainingExerciseArrayList填充正确,因为我在调试器下看了它。现在我想在RecyclerView中显示这些数据,如下所示:

public ArrayList<TrainingExercise> getData()
{
    ArrayList<TrainingExercise> it = new ArrayList<>();

    TrainingDay trainingDay = new TrainingDay();

    ArrayList<TrainingExercise> trainingExerciseArrayListnings = trainingDay.getExercises();

    for (int i = 0; i < trainingExerciseArrayListnings.size(); i++)
    {
        TrainingExercise items1 = new TrainingExercise();
        items1.getName();
        it.add(items1);
    }

    return it;
}

但这并不奏效。应用程序崩溃了。我究竟做错了什么?

我收到此错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference

0 个答案:

没有答案