public class ContactInfo implements Parcelable { protected int imageEnterprise; protected String nameEnterprise; protected String titleStreet; protected int imageLike; protected String amountLikes; protected String titleDate; protected String amountDays; public ContactInfo() { } private ContactInfo(Parcel in) { imageEnterprise = in.readInt(); nameEnterprise = in.readString(); titleStreet = in.readString(); imageLike = in.readInt(); amountLikes = in.readString(); titleDate = in.readString(); amountDays = in.readString(); } public static final Parcelable.Creator<ContactInfo> CREATOR = new Parcelable.Creator<ContactInfo>() { public ContactInfo createFromParcel(Parcel in) { return new ContactInfo(in); } public ContactInfo[] newArray(int size) { return new ContactInfo[size]; } }; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeInt(imageEnterprise); dest.writeString(nameEnterprise); dest.writeString(titleStreet); dest.writeInt(imageLike); dest.writeString(amountLikes); dest.writeString(titleDate); dest.writeString(amountDays); } } 

In the MainActivity I write the following code:

  ArrayList<ContactInfo> list1 = new ArrayList<>(); ContactInfo c = new ContactInfo(); c.imageEnterprise = R.drawable.image_list; c.nameEnterprise = "Name"; c.titleStreet = "Some street"; c.imageLike = R.drawable.image_like; c.amountLikes = "45"; c.titleDate = "3434"; c.amountDays = "5"; list1.add(c); TabFragment1 tabfragment1 = new TabFragment1(); Bundle args = new Bundle(); args.putParcelableArrayList("ttt", list1); 
  • what error is coming - Android Android
  • java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size ()' on a null object reference - Dmitry
  • in general, myList = null - Dmitry
  • and get in the fragment how? - Android Android
  • myList = this.getArguments (). getParcelableArrayList ("ttt"); - Dmitry

2 answers 2

add to activit where you create a snippet

 tabfragment1.setArgum 

I copied your code, everything works, in a fragment

 public class TabFragment1 extends Fragment { ArrayList<ContactInfo> myList; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); myList = getArguments().getParcelableArrayList("ttt"); myList.size(); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment, container, false); return view; } } 

enter image description here

  • I have this code! - Dmitry
  • copied your code, everything works, updated the answer - Yury Pashkov
  • I tried the same code only in the onCreateView method! Is this a mistake? and why write myList.size () separately; ? setArguments I have of course thanks a lot for the answer - Dmitry
  • in onCreateView everything works fine for me too. myList.size () I just wrote this - Yury Pashkov

fix the list constructor (add class) new ArrayList < ContactInfo > ()

 ArrayList<ContactInfo> list1 = new ArrayList<ContactInfo>(); 
  • the same thing happens - Dmitry
  • Try changing the class attributes from protected to private, as well as (or) for the class constructor for parcel also include public. I am making assumptions based on this answer in a similar question on stackoverflow.com/a/6681784/1767246 - xzorkiix
  • changed, added get and set methods for all fields, changed access to the constructor, and this code also tried (from the link): Bundle extras = getActivity (). getIntent (). getExtras (); myList = extras.getParcelableArrayList ("ttt"); nothing helped, still myList = null - Dmitry
  • Do you have a setArguments ()? - woesss
  • @Dmitry Update the sample code. There is no question about setArguments (), as well as about this.getArguments (). GetParcelableArrayList ("ttt"); (and there is only in the comments, the picture is not complete). - xzorkiix