0

I have Two Dynamic Fragments associated with one activity, I am trying to pass one Text from First Fragment to Second Fragment using Bundle, but I am getting Null Pointer Exception. Is it the right way to pass String between two fragments? Below is my code :

First Fragment

public class FirstFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {



        View view = inflater.inflate(R.layout.content_main, container, false);

        String text = "GetThisStringInSecondFragment";

        TextView txtView = null;
        txtView = (TextView) view.findViewById(R.id.firstfragmenttext);
        txtView.setText(text);

        Bundle bundle = new Bundle();
        bundle.putString("HI", text);

        return view;
    }


}

Second Fragment

public class SecondFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.content_secondmain,
                container, false);

        TextView txtView = null;
        txtView = (TextView) view.findViewById(R.id.secondfragmenttext);


        Bundle bundle = this.getArguments();
        String myInt = bundle.getString("HI");

        txtView.setText(myInt);

        return view;
    }


}

Activity

public class MainActivity extends AppCompatActivity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnLoad = (Button) findViewById(R.id.btn_load);

        View.OnClickListener listener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                FirstFragment hello = new FirstFragment();
                fragmentTransaction.add(R.id.fragment_container, hello, "HELLO");
                fragmentTransaction.commit();

                FragmentManager fragmentManager2 = getFragmentManager();
                FragmentTransaction fragmentTransaction2 = fragmentManager2.beginTransaction();
                SecondFragment hello2 = new SecondFragment();
                fragmentTransaction2.add(R.id.fragment_container, hello2, "HELLO");
                fragmentTransaction2.commit();
            }
        };

        btnLoad.setOnClickListener(listener);

    }


}
user2568219
  • 95
  • 3
  • 11

1 Answers1

0

You are doing getArguments in second fragment but where are you sending those arguments from first fragment. You can use Interface here and define it in activity. From activity, you can use setArgument() method for second fragment.

check the following link for more detailed information and steps to do that. https://developer.android.com/training/basics/fragments/communicating.html

Ani
  • 1,041
  • 6
  • 15