0

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

  • Please post your logcat error output. – Rohit5k2 Jun 13 '16 at 17:32
  • Trying to do ` Toast.makeText(getContext(),String.valueOf(distances.size()),Toast.LENGTH_SHORT).show();` it's shown `java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference`' – Andrea Rubi Jun 13 '16 at 17:34
  • Try getting the arguments onCreate – Arpit Ratan Jun 13 '16 at 17:34
  • Where are you showing your toast? Paste the code please. – earthw0rmjim Jun 13 '16 at 17:35
  • I've already tried to getting the arguments onCreate and it doesn't work. I've tried to show my toast in the `if(params!=null)` after getting the arraylists. – Andrea Rubi Jun 13 '16 at 17:42
  • Show us where you actually show the `Fragment` - for instance after creating `Fragment` and then setting its arguments `Bundle` - the code that you showed [ `getItem()` ] does not show us much. For instance the code where you use `FragmentManager` to add/replace a Fragment. – ishmaelMakitla Jun 13 '16 at 17:53

0 Answers0