Problems when passing a data from an Edittext in a Fragment to a Textview in another Fragment

Asked

Viewed 414 times

1

I have a Fragment containing a Edittext in which the user will enter his name.

In another Fragment is the Textview that will receive the name typed in Fragment previous.

Fragment Edit (where the user will enter the name):

public class Editar extends Fragment implements View.OnClickListener{ 
    private EditText editar; private Button okBotao;

    View rootview;

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

        editar = (EditText) rootview.findViewById(R.id.editar); 
        okBotao = (Button) getActivity().findViewById(R.id.ok);

        okBotao.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) { 
                InicioActivity menu = new InicioActivity(); 
                Bundle args = new Bundle(); 
                args.putString("editar", String.valueOf(editar));    
                menu.setArguments(args); 
            } 
         }); 
         return rootview;
    }
}

Fragment Start (where the name will be displayed):

public class Inicio extends Fragment { 
    TextView text; 
    View rootview;

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

        TextView text = (TextView)getView().findViewById(R.id.campoNome);
        String editar = getArguments().getString("editar"); 
        text.setText(editar);

        return rootview; 
    }
}

In my application Navigation Drawer.

1 answer

1


The problem is the way you try to get the string from Edittext.

String.valueOf(editar)  

The method String.valueOf() returns the representation in string of the value passed as argument.
It is usually used to convert numeric values into string.
When used with a Object, as is the case here, is the equivalent of calling the method object.toString().

Usually what object.toString() returns is not what we are waiting for. So if we use editar.toString(); will not retrieve the text from Edittext.

What is "inside" of a Edittext is not a string but rather a Editable containing the text entered in Editbox.
To access this text we must first get the Editable, using the method getText(), and then get the string with toString() :

editar.getText().toString();  

You should change the line:

args.putString("editar", String.valueOf(editar));

for

args.putString("editar", editar.getText().toString()); 
  • Thank you for helping me. But my application is still not working. This error message displays: FATAL EXCEPTION: main java.lang.Runtimeexception: Unable to start Activity

  • In the code you posted I don’t see any mTransition I suggest you ask another question with the corrected code and error log.

  • The Bundle is null. I found it. The code I sent is the one I’m using, the only change I made was the one you said.

  • So that’s the problem. Refer to the new question that when using getArguments() this is returning null.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.