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 ?

0

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

9 Comments

Can you give an example ?
But this is not a activity I am using fragments.
You are using fragment ? .... ok .... check my edit .... i have passed the integer similarly you can pass arraylist too ... simple !
@Tolgay Toklar .... download the project from my edit3 and learn how to use newInstance .... let me know if you are able to resolve it !
I think I can't do it.Is there another way to pass custom arraylist ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.