I have an issue when I try to pass some ArrayList from an activity to a fragment and I can't find errors; I have found out many answers but can't find anything that works. Here's my code, I'm doing this onResponse from a connection request to a php, it works fine and it puts the correct values in my arraylists:
for (int i = 0; i < res.length(); i++) {
try {
String data = res.getString(i);
String[] items = data.split(";");
types.add(items[0]);
distances.add(items[1]);
startDates.add(items[2]);
elapsedTimes.add(items[3]);
ratings.add(items[4]);
} catch (JSONException e) {
e.printStackTrace();
}
}
bundle.putStringArrayList("types",types);
bundle.putStringArrayList("distances",distances);
bundle.putStringArrayList("startDates",startDates);
bundle.putStringArrayList("elapsedTimes",startDates);
bundle.putStringArrayList("ratings",ratings);
}
Then, in my main activity, I have this to pass the arrays to my fragment:
@Override
public Fragment getItem(int position) {
switch (position){
/*other cases*/
case 1:
MyFragment f = new MyFragment();
f.setArguments(bundle);
return f;
}
}
Finally, in my fragment, to take bundle I do:
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Bundle params = this.getArguments();
if(params!=null) {
ratings = getArguments().getStringArrayList("ratings");
distances = params.getStringArrayList("distances");
elapsedTimes = params.getStringArrayList("elapsedTimes");
types = params.getStringArrayList("types");
startDates = params.getStringArrayList("startDates");
}
}
But, if I try to print any values in the fragment of the arrays, it's trown the NullPointerException