-1

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)
1
  • 1
    Please stop using the [tag] markdown for class names. Instead, use the proper name - e.g., ArrayList - and surround it with backticks, ` - ArrayList.
    – Mike M.
    Feb 15, 2018 at 6:34

5 Answers 5

1

Problem is your are not set string values but your trying to receive that value. that why your getting null pointer exception .Pass string value also.

TempUserStatusInfo info=new TempUserStatusInfo();
                       Bundle bundle=new Bundle();
                       bundle.putParcelable("myList",c) 
    bundle.putString("name",value);
    bundle.putString("people",value);
    bundle.putString("estimate",value);
1

Use correct arraylist to send parceable arraylist not the class object

putParcelableArrayListExtra("list", infoArrayList);

where custom object arraylist is

ArrayList<CurrentInfo> infoArrayList;

in Recieving Fragment class use this

ArrayList<CurrentInfo> infoArrayList = bundle.getParcelableArrayListExtra("list");
4
  • It is giving me error as Imcompatible Types
    – Sam
    Feb 15, 2018 at 6:55
  • did u use this class CurrentEntry ? Feb 15, 2018 at 7:37
  • Yes it s my Custom object for ArrayList
    – Sam
    Feb 15, 2018 at 8:36
  • okay make it parceable and use it Feb 15, 2018 at 9:18
0

Another idea is to use Gson to make json object and then make string of it send this string and on other side again make object from that json string using Gson.

Convert Java object to JSON

Gson gson = new Gson();
Staff obj = new Staff();
String jsonInString = gson.toJson(obj);

Revert from json to object:

Gson gson = new Gson();
Staff staff = gson.fromJson(jsonInString, Staff.class);

Add following dependency to your gradle file:

<dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.6.2</version>
</dependency>

By doing this you dont have to serialize/deserialize and you can send it as string.

0

You can use Serializable for this

//for putting in bundle
bundle.putSerializable("key", class_value)

//for receieving
object = arguments.getSerializable("key") as className

Do not forget to extend the class with serializable.

0

Try like this to pass arraylist one fragment to another

your first Fragment

ArrayList<ParcelableRow> parceArrayList = new ArrayList<ParcelableRow>();
Bundle bundle=new Bundle(); bundle.putParcelableArrayListExtra("yourArraylist",parceArrayList);

get Arraylist in other Fragment

ArrayList<ParcelableRow> result = data.getParcelableArrayList("yourArraylist");
10
  • I am getting error when i receive ArrayList Incompatible types. Required: ArrayList <App.Model.CurrentInfo> Found: ArrayList <App.Model.CurrentInfo & android.os.Parcelable>
    – Sam
    Feb 15, 2018 at 7:21
  • how you receiving arraylist can you please show the code?
    – Bunny
    Feb 15, 2018 at 8:36
  • ArrayList<CurrentInfo> current=getArguments().getParcelableArrayList("MyList");
    – Sam
    Feb 15, 2018 at 8:37
  • did u implement Parcelable in your CurrentInfo class ?
    – Bunny
    Feb 15, 2018 at 8:55
  • No.Is there a case I need to implement Parcelable in both CurrentEntry and CurrentInfo ?
    – Sam
    Feb 15, 2018 at 8:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.