I want to pass the value that a user puts on an edittext to a textview that is in another activity (the textview is in the main one, and the edittext in the second).
This code belongs to the second activity, where is the edittext where the user puts a string and this must be passed to a Textview of the MainActivity:
public class Activitat2 extends AppCompatActivity {
Button botoAcceptar;
Button botoCancelar;
EditText edittextCarrer;
TextView valorCarrer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activitat2);
widgets();
botoCancelar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Activitat2.this, MainActivity.class));
}
});
botoAcceptar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dadesAfegidesUsuari();
startActivity(new Intent(Activitat2.this, MainActivity.class));
}
});
}
public void widgets() {
botoAcceptar = (Button)findViewById(R.id.botoAcceptar);
botoCancelar = (Button)findViewById(R.id.botoCancelar);
edittextCarrer = (EditText)findViewById(R.id.edittextCarrer);
valorCarrer = (TextView)findViewById(R.id.valorUsuariCarrer);
}
public void dadesAfegidesUsuari() {
if(edittextCarrer.getText().toString().trim().length() > 0){
valorCarrer.setText(edittextCarrer.getText());
}
}}
Basically when the user has put a string in the edittext "edittextCarrer", when clicking on the button "botoAcceptar" the MainActivity should appear with the textview called "valueCarrer" with the string passed by "edittextCarrer".
It seems quite basic, and when I worked with such things in the same activity, I had no problems. But with this there is no way, I imagine that there is something I do wrong in the function "dadesAfegidesUsuari" which is responsible for passing the data.
I clarify that the id and others are well placed, it detects them all. The program crashes by clicking on the button, and indicates that the problem is in the following line:
valorCarrer.setText(edittextCarrer.getText());
The exception that appears on the monitor is:
Process: practica.m08_eac1, PID: 2707 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText (java.lang.CharSequence)' on a null object reference at practica.m08_eac1.Activitat2.dadesAfegidesUsuari (Activitat2.java:73) at practica.m08_eac1.Activitat2 $ 2.onClick (Activitat2.java:47) at android.view.View.performClick (View.java:5637) at android.view.View $ PerformClick.run (View.java:22429) at android.os.Handler.handleCallback (Handler.java:751) at android.os.Handler.dispatchMessage (Handler.java:95) at android.os.Looper.loop (Looper.java:154) at android.app.ActivityThread.main (ActivityThread.java:6119) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:776)