1

How to pass through Parcelable, an object of type List In the fragment through the Bundle of Activity? Constantly produces an error type NPE, In the preparation Parcelable in OnCreateView.

// Fragment object в Activity
public Fragment systemFragList(){
          a = new AppsManagerFragment();
          b = new Bundle();
          dApps = new DataApps(this);
          //transmission through List the designer
          listCreater = new MakingListObject(dApps.getSystemAppsList()); 
          b.putParcelable("List", listCreater); // transfer Parcelable object through Bundle
          a.setArguments(b);

        return a;
    };

// Parcelable class
public class MakingListObject implements Parcelable {

    private List<ApplicationInfo> list;

 // get an object List <ApplicationInfo> constructor
    public MakingListObject(List<ApplicationInfo> l) { 
        this.list = l;
    }
    public MakingListObject(Parcel in) {
        in.readTypedList(list, ApplicationInfo.CREATOR); // read
    }
    public List<ApplicationInfo> getList(){ // vethod return list
        return list;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) { // write
       dest.writeTypedList(list);
    }
    @Override
    public int describeContents() {
        return 0;
    }

   CREATOR...
}

//Admission List to the Fragment in onCreateView()    
MakingListObject obj = (MakingListObject)this.getArguments().getParcelable("List"); // NPE ERROR
List<ApplicationInfo> listData = obj.getList();
Dimas
  • 23
  • 1
  • 6

2 Answers2

2

store list of Parcelable using

Bundle dataToSend = new Bundle();
ArrayList<? super Parcelable> list = new ArrayList<>();

//add items here

and read it using

Bundle receivedData = ...
receivedData.getParcelableArrayList("foo")

hope this helps

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
Lakshan Dissanayake
  • 521
  • 1
  • 4
  • 18
0

You have to init the list first.

public MakingListObject(Parcel in) {
    list = new ArrayList<ApplicationInfo>;
    in.readTypedList(list, ApplicationInfo.CREATOR); // read
}
elcolto
  • 961
  • 7
  • 11
  • The code can be seen that the constructor initializes the list. Though so I also tried and still there NPE. – Dimas Jan 12 '15 at 14:06