-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)
ADM
  • 20,406
  • 11
  • 52
  • 83
Sam
  • 39
  • 7
  • 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 '18 at 06:34

5 Answers5

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);
sasikumar
  • 12,540
  • 3
  • 28
  • 48
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");
Quick learner
  • 10,632
  • 4
  • 45
  • 55
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.

Dmitriy
  • 5,525
  • 12
  • 25
  • 38
Harsh Agrawal
  • 597
  • 3
  • 12
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.

Muhammad Saad Rafique
  • 3,158
  • 1
  • 13
  • 21
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");
Bunny
  • 1,044
  • 12
  • 23
  • I am getting error when i receive ArrayList Incompatible types. Required: ArrayList Found: ArrayList – Sam Feb 15 '18 at 07:21
  • how you receiving arraylist can you please show the code? – Bunny Feb 15 '18 at 08:36
  • ArrayList current=getArguments().getParcelableArrayList("MyList"); – Sam Feb 15 '18 at 08:37
  • did u implement Parcelable in your CurrentInfo class ? – Bunny Feb 15 '18 at 08:55
  • No.Is there a case I need to implement Parcelable in both CurrentEntry and CurrentInfo ? – Sam Feb 15 '18 at 08:58
  • you need to implement Parcelable to get the arraylist in another fragement.try and let me know does it make any difference or not – Bunny Feb 15 '18 at 09:00
  • Now I am done with this.That error resolved. Now how to get and set the data in current fragment ? – Sam Feb 15 '18 at 09:01
  • you got the list now read understand do it by your own.thank you – Bunny Feb 15 '18 at 09:04
  • I got this error Attempt to invoke virtual method 'java.util.ArrayList android.os.Bundle.getParcelableArrayList(java.lang.String)' on a null object reference – Sam Feb 15 '18 at 09:18
  • we can only show you the path how do things rest of things you need to do it by your own try to debug the code and see where things going wrong.thank you – Bunny Feb 15 '18 at 09:29