I am passing the ArrayList
from one Fragment
to another.I am using Parcelable
to achieve this.I am passing ArrayList
using Bundle
.But in another Fragment I am not able to receive this.
This is code for Class which implements Parelable
public class CurrentEntry implements Parcelable{
String name;
String people;
String estimate;
public CurrentEntry()
{
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPeople() {
return people;
}
public void setPeople(String people) {
this.people = people;
}
public String getEstimate() {
return estimate;
}
public void setEstimate(String estimate) {
this.estimate = estimate;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.name);
dest.writeString(this.people);
dest.writeString(this.estimate);
}
public static final Parcelable.Creator<CurrentEntry> CREATOR
= new Parcelable.Creator<CurrentEntry>() {
public CurrentEntry createFromParcel(Parcel in) {
return new CurrentEntry(in);
}
public CurrentEntry[] newArray(int size) {
return new CurrentEntry[size];
}
};
public CurrentEntry(Parcel in)
{
this.name=in.readString();
this.people=in.readString();
this.estimate=in.readString();
}
}
This is code for sending ArrayList from one Fragment to Another.
JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try
{
JSONObject object=new JSONObject(response.toString());
JSONArray data=object.getJSONArray("results");
for( i=0;i<data.length();i++)
{
JSONObject json = data.getJSONObject(i);
final CurrentEntry c=new CurrentEntry();
userUrl=json.getString("list");
JsonObjectRequest request=new JsonObjectRequest(Request.Method.GET, userUrl, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject temp=response.getJSONObject("user");
String foodie_name=temp.getString("first_name");
String lname=temp.getString("last_name");
c.setName(name+"\t"+lname);
progressDialog.dismiss();
} catch (JSONException e) {
e.printStackTrace();
}
// adapt.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
})
AppController.getInstance().addToRequestQueue(request,second_req);
c.setPeople(json.getString("no_people"));
String temp=json.getString("predicated_t");
c.setEstimate(temp+"min");
// c.setNo(count);
TempUserStatusInfo info=new TempUserStatusInfo();
Bundle bundle=new Bundle();
bundle.putParcelable("myList",c);
info.setArguments(bundle);
current.add(c);
adapt=new NewAdapter(current,getActivity().getApplicationContext());
recyclerView.setAdapter(adapt);
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
This is code for receiving the ArrayList where I am setting SwipeCards
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v=inflater.inflate(R.layout.temp_user_status_info, container, false);
swipeCardsView=(SwipeCardsView)v.findViewById(R.id.swipeCard);
infoAdapter=new CurrentInfoAdapter(info,getActivity());
swipeCardsView.retainLastCard(false);
swipeCardsView.enableSwipe(true);
CurrentInfo currentInfo=new CurrentInfo();
Bundle bundle=this.getArguments();
if(bundle!=null){
currentInfo=bundle.getParcelable("myList");
}
person_name=getArguments().getString("name");
person_people=getArguments().getString("people");
person_estimate=getArguments().getString("estimate");
currentInfo.setName(person_name);
currentInfo.setPeople(person_people);
currentInfo.setEstimate(person_estimate);
Log.i("name",person_name);
Log.i("people",person_people);
Log.i("estimate",person_estimate);
info.add(currentInfo);
swipeCardsView.setAdapter(infoAdapter);
return v;
}
The error is
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.BaseBundle.getString(java.lang.String)' on a null object reference
at biz.fyra.QueueApp.TempUserStatusInfo.onCreateView(TempUserStatusInfo.java:52)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2248)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1340)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1569)