Passing ArreyList between Fragments with Parcelabl

2019-06-12 17:04发布

问题:

After consuming a json with Retrofit and using unsynchronous callback I can't pass An ArrayList from The MainActivity to a fragment .

Code from MainActivity:

 lFragmentManager = getFragmentManager();
            lFragment = lFragmentManager.findFragmentById(R.id.frame_container);
            lFragment = new Fragment_Categories();
            Bundle bundle = new Bundle();
            bundle.putParcelableArrayList("list_categories", Categories_.getCategories());
            //for(int a = 0 ; a < Categories_.getCategories().size(); a++)
            //   Log.d("billy",Categories_.getCategories().get(a).getTitle());
            lFragment.setArguments(bundle);
        lFragmentManager.beginTransaction().replace(R.id.frame_container ,lFragment ).commit();

Note that the Log inside comments does print the context of the list, so I take the ArrayList in the fragment with this code inside onCreateView:

if(savedInstanceState != null)
        categories =  savedInstanceState.getParcelableArrayList("list_categories");

    /*
     * initialize the Recycler view
     */
    mRecycler = (RecyclerView)view.findViewById(R.id.categories_list);
    mAdapter = new AdapterCategories(categories,getActivity());
    mRecycler.setAdapter(mAdapter);

Here is my Class Category :

public class Categories implements Parcelable{

private ArrayList<NavDrawerItem> categories;

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeTypedList(categories);

}

public static final Parcelable.Creator<Categories> CREATOR = new Parcelable.Creator<Categories>() {

    public Categories createFromParcel(Parcel in) {
        return new Categories(in);

    }

    public Categories[] newArray(int size){
        return new Categories[size];
    }
};

private Categories(Parcel in){

    categories = in.createTypedArrayList(NavDrawerItem.CREATOR);
}

public ArrayList<NavDrawerItem> getCategories() {
    return categories;
}
}

And here is the Class NavDrawerItem:

public class NavDrawerItem implements Parcelable {

private String title;
private String description;
private String image;
private String post_count;
private String id;
private String slug;
private int parent;

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(title);
    dest.writeString(description);
    dest.writeString(image);
    dest.writeString(post_count);
    dest.writeString(id);
    dest.writeString(slug);
    dest.writeInt(parent);
}

public static final Parcelable.Creator<NavDrawerItem> CREATOR = new Parcelable.Creator<NavDrawerItem>() {

    public NavDrawerItem createFromParcel(Parcel in) {
        return new NavDrawerItem(in);

    }

    public NavDrawerItem[] newArray(int size){
        return new NavDrawerItem[size];
    }
};

private NavDrawerItem(Parcel in){
    title = in.readString();
    description = in.readString();
    image = in.readString();
    post_count = in.readString();
    id = in.readString();
    slug = in.readString();
    parent = in.readInt();
}

The problem is that inside fragment (when trying to pass the list to the Adapter of the RecyclerView) or inside the adapter of the RecyclerView I take a null exception :

java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList android.os.Bundle.getParcelableArrayList(java.lang.String)' on a null object reference

Thank you for the help!!

回答1:

You wouldn't use the savedInstanceState to get your arguments, because they weren't passed as part of a saveInstanceState() operation. You want to use getArguments() instead. So try this...

categories = getArguments().getParcelableArrayList("list_categories");