I have written following code in my Activity Class,
ArrayList<TXData> data;
data= Map.getDataList();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("data",data);
Fragment fragment = new MapsFragment();
fragment.setArguments(bundle);
In MapsFragment
, I have written following code to get data on onActivityCreated()
Method.
ArrayList<TXData> dataList;
dataList=getArguments().getParcelableArrayList("data");
It returns following exception on Fragment class.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method java.util.ArrayList android.os.Bundle.getParcelableArrayList(java.lang.String) on a null object reference
I added debug points, to find out the cause of problem, It successfully adds the data to Bundle from Activity
, but its not getting any data in Fragment.
Kindly guide me what I am doing wrong here.
EDIT Parceable Class
public class TXData implements Serializable, Parcelable{
public String id;
public String title;
public String pId = "";
public float heading;
public float pitch;
public int totalCount;
int mData;
public TXSData(){
}
protected TXData(Parcel in) {
id = in.readString();
title = in.readString();
pId = in.readString();
heading = in.readFloat();
pitch = in.readFloat();
totalCount = in.readInt();
mData = in.readInt();
minPrice = in.readInt();
maxPrice = in.readInt();
isInFilter = in.readByte() != 0;
isEnabled = in.readByte() != 0;
totalCount = in.readInt();
}
public static final Creator<TXData> CREATOR = new Creator<TXData>() {
@Override
public TXData createFromParcel(Parcel in) {
return new TXData(in);
}
@Override
public TXData[] newArray(int size) {
return new TXData[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(mData);
}
Try getting the Bundle object (
getArguments()
) inonCreate()
oronCreateView()
of the fragment and notonActivityCreated()
.Refer to the example from Fragment documentation.