1

I have a class like this

class Message implements Serializable
{
    public String message, sender;
    public Message (String msg, String from)
    {
        // check here that msg and from are not null. If so, use a default value or throw an NullPointerException
        message = msg;
        sender = from;
    }
    // Just a utility method for printing out the Message to System.out
    public String toString () { return sender+":"+message; }
}

I am defining this variable in main activity

Hashtable<String, ArrayList<Message>> table = new Hashtable<String, ArrayList<Message>>();
table.get(room_name).add(new Message("Hi", "Sender"));
Bundle bundle = new Bundle();
bundle.putSerializable("messages", table.get(sendTo));

And in fragment I am getting this data with this code

ArrayList<Message> extractedMessages = (ArrayList<Message>)getArguments().getSerializable("messages");
System.out.println(extractedMessages.size());

But my app is crashing,I am getting java nullpointer exception for extractedMessages.size()

How can I fix this ?

Tolgay Toklar
  • 4,151
  • 8
  • 43
  • 73

1 Answers1

2

Solution is ::

Bundle.putParcelable/Bundle.getParcelable

Have a look at this answer.

  • Use those methods, putParcelable and getParcelable.

Also you can ::

Intent intent = new Intent(context, SecondActity.class);
intent.putIntegerArrayListExtra("arraylist",myArrayList);  //myArrayList is ArrayList<Integer>
startActivity(intent);

To get the arrayList in second Activity.

ArrayList arrayList<Integer> = getIntent().getIntegerArrayListExtra("myArrayList")

{Edit} ---- You can use newInstance to pass the data from fragment to fragment

This is one of the way to achieve it, i use constructor to pass the data

FragmentOne.java

int myData=12;
FragmentManager manager = getActivity().getSupportFragmentManager();
Fragment frgObj=FragmentTwo.newInstance(myData);
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.container, frgObj,"FragmentTwo");
ft.addToBackStack(null);
ft.commit();

FragmentTwo.java

int myData;
public static FragmentTwo newInstance(int _myData){
FragmentTwo fragment = new FragmentTwo();
myData=_myData
return  fragment;
}

{EDIT-3}

I have made a project for you download it from Here

  • Mount in in your editor and run it .... it has bunch of other stuffs .... don't bother about it
  • Check how i am starting a fragment from activity ----> then onClick of button how i am starting a fragment ---> there pass the data with the constructor as i shown in edit2

ALSO REFER -- this -- StackOVERFLOW POST

Community
  • 1
  • 1
Devrath
  • 42,072
  • 54
  • 195
  • 297
  • Can you give an example ? – Tolgay Toklar Aug 10 '14 at 17:34
  • But this is not a activity I am using fragments. – Tolgay Toklar Aug 10 '14 at 17:37
  • You are using fragment ? .... ok .... check my edit .... i have passed the integer similarly you can pass arraylist too ... simple ! – Devrath Aug 10 '14 at 17:41
  • @Tolgay Toklar .... download the project from my `edit3` and learn how to use `newInstance` .... let me know if you are able to resolve it ! – Devrath Aug 10 '14 at 17:59
  • I think I can't do it.Is there another way to pass custom arraylist ? – Tolgay Toklar Aug 10 '14 at 18:04
  • using newInstance is the simplest way , ...else you have to perform interfragmentcommunication...pass data from fragmenta--->activity ----> fragmentb ... check this tutorial https://www.youtube.com/watch?v=GDHnIZEJ76I ........... my way is very simpler ! – Devrath Aug 10 '14 at 18:08
  • But I am using tab system,and I don't want to change my methods.Did you look my code ? Can I implement your code to my code ? – Tolgay Toklar Aug 10 '14 at 18:21
  • Yes you can ! ... using `newinstance` is just using constructor to pass the data ... try some sample examples on using `newinstance` ... my download link project has all you need .... – Devrath Aug 10 '14 at 18:33
  • I tried now,but not worked,I could pass the list to another fragment but I am still can't using size function. – Tolgay Toklar Aug 14 '14 at 18:34