Pass data between two fragments [duplicate]

0

I'm doing Navigation Draw Activity , with fragments inside. The fact is that I have several fragments. It is a long form that I divided into several fragments, and I want to send the data I receive from editText of the first fragmentm, get them in the following fragment. I am using bundles , I had already used it in activities but in fragments it is different. The point is that when I test my application, when I get to the fragment where in theory I should receive the data.

The application closes and displays the following error message:

  

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString (java.lang.String)' on a null object reference

I share part of my code, I do not know what I'm doing wrong. It should be noted that the goal of passing the data from one fragment to another , is that in that last fragment receive data from several fragments and then insert it into a BD.

Fragment where I sent the data:

  bSiguiente.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {





            /*Bundle data = new Bundle();
            data.putString("folio", folio.getText().toString());

            FragmentTransaction t = getActivity().getSupportFragmentManager()
                    .beginTransaction();
            SherlockListFragment mFrag = new ProfileFragment();
            mFrag.setArguments(data);
            t.replace(R.id.main_frag, mFrag);
            t.commit();*/

            Bundle data = new Bundle();
            data.putString("userprofile", folio.getText().toString());
            FragmentTransaction t = getActivity().getSupportFragmentManager()
                    .beginTransaction();
            CapturarInstalaciones6Fragment mFrag = new CapturarInstalaciones6Fragment();
            mFrag.setArguments(data);

            t.commit();

Fragment where you should receive the data (I show it in a Toast to test it):

 bSiguiente.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Bundle extras = getArguments();
            String s = extras.getString("userprofile");
            Toast.makeText(getActivity().getApplicationContext(), "Folio: "+ s, Toast.LENGTH_SHORT).show();

            FragmentTransaction trans = getFragmentManager().beginTransaction();
            trans.replace(R.id.contenedor, new CapturarInstalaciones7Fragment());
            trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            trans.addToBackStack(null);
            trans.commit();

        }
    });
    
asked by Javier Aguilar 06.12.2017 в 17:26
source

1 answer

-1

Inside this listener can not be obtained, for that reason extras have a null value:

bSiguiente.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Bundle extras = getArguments();
            String s = extras.getString("userprofile");
            ...
            ...

You can get the data as another option within onCreate() and use it in any other part of the application, for example within the listener.

private String s = "";

@Override
    public void onCreate(Bundle savedInstanceState) {
         Bundle extras = getArguments();
         s = extras.getString("userprofile");
         ...
         ...

     bSiguiente.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //Bundle extras = getArguments();
            //String s = extras.getString("userprofile");

            Toast.makeText(getActivity().getApplicationContext(), "Folio: "+ s, Toast.LENGTH_SHORT).show();

            FragmentTransaction trans = getFragmentManager().beginTransaction();
            trans.replace(R.id.contenedor, new CapturarInstalaciones7Fragment());
            trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            trans.addToBackStack(null);
            trans.commit();

        }
    });


   }
    
answered by 06.12.2017 в 17:47