I have in my main_activity.xml
EditText
and a Button
.
Inside the EditText
I insert a name and once I click the Button
a new activity with new layout appears.
Now, I had like to use the value from the EditText inside my new activity (hiscores.xml
) however the app crashes.
My MainActivity is as follows:
package com.example.test;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.opening_screen);
button = findViewById(R.id.continue_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openHiScore();
}
});
}
public void openHiScore(){
Intent intent = new Intent(this,HiScore.class);
startActivity(intent);
}
}
Then, inside HiScore the code is;
package com.example.test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
public class HiScore extends AppCompatActivity {
EditText Name= (EditText)findViewById(R.id.username_text);
String UserName = Name.getText().toString();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hiscore);
}
}
How do I correctly obtain the value inserted into the EditText
to use it inside the string UserName
?
Thank you!