Send text from EditText to a TextView of another class

1

I try to send a text that I write in my editText and receive it in TextView that I have in another clase but I close the application, is not possible to do this, only Can you send a TextView of the same clase ?

This is the logcat :

05-17 01:50:06.035 20106-20106/otrointento.dos E/AndroidRuntime: FATAL EXCEPTION: main
    Process: otrointento.dos, PID: 20106
    Theme: themes:{default=overlay:com.saalim.cm.theme.dusk, iconPack:com.saalim.cm.theme.dusk, fontPkg:com.saalim.cm.theme.dusk, com.android.systemui=overlay:com.saalim.cm.theme.dusk, com.android.systemui.navbar=overlay:com.saalim.cm.theme.dusk}
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
    at otrointento.dos.Clase1$1.onClick(Clase1.java:28)
    at android.view.View.performClick(View.java:5204)
    at android.view.View$PerformClick.run(View.java:21158)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5461)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)

This is how I try, clase1 :

public class Clase1  extends ActionBarActivity {

    EditText mensaje;
    Button btn1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clase1);

        mensaje = (EditText)findViewById(R.id.mensaje);
        btn1 = (Button)findViewById(R.id.btn1);

        // boton para realizar la acción

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // envio el texto introducido en mi EditText

                String dato = mensaje.getText().toString();
                Clase2.receptor.setText("Hola " +dato);

                // Abro la Clase2

                Intent c1 = new Intent(Clase1.this, Clase2.class);
                startActivity(c1);
            }
        });

    }
}

And here the other clase2 :

public class Clase2 extends AppCompatActivity {

    static TextView receptor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clase2);

        // en este TextView quiero recibir el texto

        receptor = (TextView) findViewById(R.id.receptor);
    }
}
    
asked by UserNameYo 17.05.2017 в 01:57
source

1 answer

1

The correct way to send data between Activities is through a bundle :

  String dato = mensaje.getText().toString(); //Obtienes el texto del EditText
  Intent c1 = new Intent(Clase1.this, Clase2.class);
  c1.putExtra("dato", dato);
  startActivity(c1);

the value you would receive it in this way:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clase2);

        // en este TextView quiero recibir el texto
        receptor = (TextView) findViewById(R.id.receptor);

        //Recepcion de datos.
        Bundle parametros = this.getIntent().getExtras();
        if(parametros !=null){
          String dato = parametros.getString("dato"); 
          receptor.setText(dato); 
        } 

    }
    
answered by 17.05.2017 / 02:14
source