I'm not getting data from another activity
up vote
-1
down vote
favorite
I am getting a problem with my project in Android Studio.
What I am trying to do is when I click a button (what opens another activity) and in that activity I write data from a person.
Then I save this data and the app get back to the main. The next button what does is create a Toast that shows data from the person, but the Toast is returning me null. I don't know what I am doing wrong.
public class PrincipalActivity extends AppCompatActivity implements View.OnClickListener {
Button btnObreActivitat1, btnToastDadesPersona;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal);
btnObreActivitat1 = findViewById(R.id.btnObreActivitat1);
btnObreActivitat1.setOnClickListener(this);
btnToastDadesPersona = findViewById(R.id.btnToastDadesPersona);
btnToastDadesPersona.setOnClickListener(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Resposta invalida", Toast.LENGTH_SHORT);
} else {
switch (requestCode) {
case 5:
setTitle(data.getStringExtra("RESPOSTA"));
break;
}
}
}
@Override
public void onClick(View v) {
Intent intencio;
StringBuilder sbUri = new StringBuilder();
switch (v.getId()){
case R.id.btnObreActivitat1:
intencio = new Intent(this,Activitat1.class);
startActivityForResult(intencio,5);
break;
case R.id.btnToastDadesPersona:
intencio = getIntent();
String nom = intencio.getStringExtra("NOM");
String cognom = intencio.getStringExtra("COGNOM");
String sexe = intencio.getStringExtra("SEXE");
Toast torrada = new Toast(this);
LayoutInflater inflador = getLayoutInflater();
View vista = inflador.inflate(R.layout.torrada,null);
TextView tvMissatge = vista.findViewById(R.id.tvMissatge);
tvMissatge.setText("Nom: " + nom + "nCognom: " + cognom + "nSexe: " + sexe);
torrada.setView(vista);
torrada.setDuration(Toast.LENGTH_SHORT);
torrada.show();
break;
}
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onResume(){
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onStop() {
super.onStop();
}
}
//Next activity where I write data :
public class Activitat1 extends AppCompatActivity {
final static String nom = "NOM";
final static String cognom = "COGNOM";
final static String sexe = "SEXE";
Button btnEnviar, btnCancelar;
EditText etNom, etCognom;
RadioButton rbtSexe;
RadioGroup rgpSexe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activitat1);
etNom = findViewById(R.id.etNom);
etCognom = findViewById(R.id.etCognom);
btnEnviar = findViewById(R.id.btnEnviar);
btnCancelar = findViewById(R.id.btnCancelar);
rgpSexe = findViewById(R.id.rgpSexe);
rbtSexe = findViewById(rgpSexe.getCheckedRadioButtonId());
btnEnviar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intencio = Activitat1.this.getIntent();
intencio.putExtra(nom, etNom.getText().toString());
intencio.putExtra(cognom, etCognom.getText().toString());
intencio.putExtra(sexe,rbtSexe.getText().toString());
startActivity(intencio);
}
});
btnCancelar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setResult(RESULT_CANCELED);
finish();
}
});
}
}
android android-studio android-intent toast android-toast
add a comment |
up vote
-1
down vote
favorite
I am getting a problem with my project in Android Studio.
What I am trying to do is when I click a button (what opens another activity) and in that activity I write data from a person.
Then I save this data and the app get back to the main. The next button what does is create a Toast that shows data from the person, but the Toast is returning me null. I don't know what I am doing wrong.
public class PrincipalActivity extends AppCompatActivity implements View.OnClickListener {
Button btnObreActivitat1, btnToastDadesPersona;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal);
btnObreActivitat1 = findViewById(R.id.btnObreActivitat1);
btnObreActivitat1.setOnClickListener(this);
btnToastDadesPersona = findViewById(R.id.btnToastDadesPersona);
btnToastDadesPersona.setOnClickListener(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Resposta invalida", Toast.LENGTH_SHORT);
} else {
switch (requestCode) {
case 5:
setTitle(data.getStringExtra("RESPOSTA"));
break;
}
}
}
@Override
public void onClick(View v) {
Intent intencio;
StringBuilder sbUri = new StringBuilder();
switch (v.getId()){
case R.id.btnObreActivitat1:
intencio = new Intent(this,Activitat1.class);
startActivityForResult(intencio,5);
break;
case R.id.btnToastDadesPersona:
intencio = getIntent();
String nom = intencio.getStringExtra("NOM");
String cognom = intencio.getStringExtra("COGNOM");
String sexe = intencio.getStringExtra("SEXE");
Toast torrada = new Toast(this);
LayoutInflater inflador = getLayoutInflater();
View vista = inflador.inflate(R.layout.torrada,null);
TextView tvMissatge = vista.findViewById(R.id.tvMissatge);
tvMissatge.setText("Nom: " + nom + "nCognom: " + cognom + "nSexe: " + sexe);
torrada.setView(vista);
torrada.setDuration(Toast.LENGTH_SHORT);
torrada.show();
break;
}
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onResume(){
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onStop() {
super.onStop();
}
}
//Next activity where I write data :
public class Activitat1 extends AppCompatActivity {
final static String nom = "NOM";
final static String cognom = "COGNOM";
final static String sexe = "SEXE";
Button btnEnviar, btnCancelar;
EditText etNom, etCognom;
RadioButton rbtSexe;
RadioGroup rgpSexe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activitat1);
etNom = findViewById(R.id.etNom);
etCognom = findViewById(R.id.etCognom);
btnEnviar = findViewById(R.id.btnEnviar);
btnCancelar = findViewById(R.id.btnCancelar);
rgpSexe = findViewById(R.id.rgpSexe);
rbtSexe = findViewById(rgpSexe.getCheckedRadioButtonId());
btnEnviar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intencio = Activitat1.this.getIntent();
intencio.putExtra(nom, etNom.getText().toString());
intencio.putExtra(cognom, etCognom.getText().toString());
intencio.putExtra(sexe,rbtSexe.getText().toString());
startActivity(intencio);
}
});
btnCancelar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setResult(RESULT_CANCELED);
finish();
}
});
}
}
android android-studio android-intent toast android-toast
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I am getting a problem with my project in Android Studio.
What I am trying to do is when I click a button (what opens another activity) and in that activity I write data from a person.
Then I save this data and the app get back to the main. The next button what does is create a Toast that shows data from the person, but the Toast is returning me null. I don't know what I am doing wrong.
public class PrincipalActivity extends AppCompatActivity implements View.OnClickListener {
Button btnObreActivitat1, btnToastDadesPersona;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal);
btnObreActivitat1 = findViewById(R.id.btnObreActivitat1);
btnObreActivitat1.setOnClickListener(this);
btnToastDadesPersona = findViewById(R.id.btnToastDadesPersona);
btnToastDadesPersona.setOnClickListener(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Resposta invalida", Toast.LENGTH_SHORT);
} else {
switch (requestCode) {
case 5:
setTitle(data.getStringExtra("RESPOSTA"));
break;
}
}
}
@Override
public void onClick(View v) {
Intent intencio;
StringBuilder sbUri = new StringBuilder();
switch (v.getId()){
case R.id.btnObreActivitat1:
intencio = new Intent(this,Activitat1.class);
startActivityForResult(intencio,5);
break;
case R.id.btnToastDadesPersona:
intencio = getIntent();
String nom = intencio.getStringExtra("NOM");
String cognom = intencio.getStringExtra("COGNOM");
String sexe = intencio.getStringExtra("SEXE");
Toast torrada = new Toast(this);
LayoutInflater inflador = getLayoutInflater();
View vista = inflador.inflate(R.layout.torrada,null);
TextView tvMissatge = vista.findViewById(R.id.tvMissatge);
tvMissatge.setText("Nom: " + nom + "nCognom: " + cognom + "nSexe: " + sexe);
torrada.setView(vista);
torrada.setDuration(Toast.LENGTH_SHORT);
torrada.show();
break;
}
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onResume(){
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onStop() {
super.onStop();
}
}
//Next activity where I write data :
public class Activitat1 extends AppCompatActivity {
final static String nom = "NOM";
final static String cognom = "COGNOM";
final static String sexe = "SEXE";
Button btnEnviar, btnCancelar;
EditText etNom, etCognom;
RadioButton rbtSexe;
RadioGroup rgpSexe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activitat1);
etNom = findViewById(R.id.etNom);
etCognom = findViewById(R.id.etCognom);
btnEnviar = findViewById(R.id.btnEnviar);
btnCancelar = findViewById(R.id.btnCancelar);
rgpSexe = findViewById(R.id.rgpSexe);
rbtSexe = findViewById(rgpSexe.getCheckedRadioButtonId());
btnEnviar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intencio = Activitat1.this.getIntent();
intencio.putExtra(nom, etNom.getText().toString());
intencio.putExtra(cognom, etCognom.getText().toString());
intencio.putExtra(sexe,rbtSexe.getText().toString());
startActivity(intencio);
}
});
btnCancelar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setResult(RESULT_CANCELED);
finish();
}
});
}
}
android android-studio android-intent toast android-toast
I am getting a problem with my project in Android Studio.
What I am trying to do is when I click a button (what opens another activity) and in that activity I write data from a person.
Then I save this data and the app get back to the main. The next button what does is create a Toast that shows data from the person, but the Toast is returning me null. I don't know what I am doing wrong.
public class PrincipalActivity extends AppCompatActivity implements View.OnClickListener {
Button btnObreActivitat1, btnToastDadesPersona;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal);
btnObreActivitat1 = findViewById(R.id.btnObreActivitat1);
btnObreActivitat1.setOnClickListener(this);
btnToastDadesPersona = findViewById(R.id.btnToastDadesPersona);
btnToastDadesPersona.setOnClickListener(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Resposta invalida", Toast.LENGTH_SHORT);
} else {
switch (requestCode) {
case 5:
setTitle(data.getStringExtra("RESPOSTA"));
break;
}
}
}
@Override
public void onClick(View v) {
Intent intencio;
StringBuilder sbUri = new StringBuilder();
switch (v.getId()){
case R.id.btnObreActivitat1:
intencio = new Intent(this,Activitat1.class);
startActivityForResult(intencio,5);
break;
case R.id.btnToastDadesPersona:
intencio = getIntent();
String nom = intencio.getStringExtra("NOM");
String cognom = intencio.getStringExtra("COGNOM");
String sexe = intencio.getStringExtra("SEXE");
Toast torrada = new Toast(this);
LayoutInflater inflador = getLayoutInflater();
View vista = inflador.inflate(R.layout.torrada,null);
TextView tvMissatge = vista.findViewById(R.id.tvMissatge);
tvMissatge.setText("Nom: " + nom + "nCognom: " + cognom + "nSexe: " + sexe);
torrada.setView(vista);
torrada.setDuration(Toast.LENGTH_SHORT);
torrada.show();
break;
}
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onResume(){
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onStop() {
super.onStop();
}
}
//Next activity where I write data :
public class Activitat1 extends AppCompatActivity {
final static String nom = "NOM";
final static String cognom = "COGNOM";
final static String sexe = "SEXE";
Button btnEnviar, btnCancelar;
EditText etNom, etCognom;
RadioButton rbtSexe;
RadioGroup rgpSexe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activitat1);
etNom = findViewById(R.id.etNom);
etCognom = findViewById(R.id.etCognom);
btnEnviar = findViewById(R.id.btnEnviar);
btnCancelar = findViewById(R.id.btnCancelar);
rgpSexe = findViewById(R.id.rgpSexe);
rbtSexe = findViewById(rgpSexe.getCheckedRadioButtonId());
btnEnviar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intencio = Activitat1.this.getIntent();
intencio.putExtra(nom, etNom.getText().toString());
intencio.putExtra(cognom, etCognom.getText().toString());
intencio.putExtra(sexe,rbtSexe.getText().toString());
startActivity(intencio);
}
});
btnCancelar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setResult(RESULT_CANCELED);
finish();
}
});
}
}
android android-studio android-intent toast android-toast
android android-studio android-intent toast android-toast
edited Nov 19 at 20:02
Ali Khaki
7111418
7111418
asked Nov 19 at 16:00
Jeyem
33
33
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
To send back the persons data you just write after finish the Activitat1, you need to call setResult, passing an intent and then call finish. Please, take a look at this reply here to see how to do it:
https://stackoverflow.com/a/947560/3342108
add a comment |
up vote
0
down vote
in btnEnviar.setOnClickListener()
you are creating a new instance startActivity(intencio);
instead of returning to the parent activity setResult(RESULT_OK, intencio);finish();
.
try this
btnEnviar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intencio = Activitat1.this.getIntent();
intencio.putExtra(nom, etNom.getText().toString());
intencio.putExtra(cognom, etCognom.getText().toString());
intencio.putExtra(sexe,rbtSexe.getText().toString());
setResult(RESULT_OK, intencio);
finish(); }
});
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
To send back the persons data you just write after finish the Activitat1, you need to call setResult, passing an intent and then call finish. Please, take a look at this reply here to see how to do it:
https://stackoverflow.com/a/947560/3342108
add a comment |
up vote
0
down vote
accepted
To send back the persons data you just write after finish the Activitat1, you need to call setResult, passing an intent and then call finish. Please, take a look at this reply here to see how to do it:
https://stackoverflow.com/a/947560/3342108
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
To send back the persons data you just write after finish the Activitat1, you need to call setResult, passing an intent and then call finish. Please, take a look at this reply here to see how to do it:
https://stackoverflow.com/a/947560/3342108
To send back the persons data you just write after finish the Activitat1, you need to call setResult, passing an intent and then call finish. Please, take a look at this reply here to see how to do it:
https://stackoverflow.com/a/947560/3342108
answered Nov 19 at 16:18
Natan
483
483
add a comment |
add a comment |
up vote
0
down vote
in btnEnviar.setOnClickListener()
you are creating a new instance startActivity(intencio);
instead of returning to the parent activity setResult(RESULT_OK, intencio);finish();
.
try this
btnEnviar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intencio = Activitat1.this.getIntent();
intencio.putExtra(nom, etNom.getText().toString());
intencio.putExtra(cognom, etCognom.getText().toString());
intencio.putExtra(sexe,rbtSexe.getText().toString());
setResult(RESULT_OK, intencio);
finish(); }
});
add a comment |
up vote
0
down vote
in btnEnviar.setOnClickListener()
you are creating a new instance startActivity(intencio);
instead of returning to the parent activity setResult(RESULT_OK, intencio);finish();
.
try this
btnEnviar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intencio = Activitat1.this.getIntent();
intencio.putExtra(nom, etNom.getText().toString());
intencio.putExtra(cognom, etCognom.getText().toString());
intencio.putExtra(sexe,rbtSexe.getText().toString());
setResult(RESULT_OK, intencio);
finish(); }
});
add a comment |
up vote
0
down vote
up vote
0
down vote
in btnEnviar.setOnClickListener()
you are creating a new instance startActivity(intencio);
instead of returning to the parent activity setResult(RESULT_OK, intencio);finish();
.
try this
btnEnviar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intencio = Activitat1.this.getIntent();
intencio.putExtra(nom, etNom.getText().toString());
intencio.putExtra(cognom, etCognom.getText().toString());
intencio.putExtra(sexe,rbtSexe.getText().toString());
setResult(RESULT_OK, intencio);
finish(); }
});
in btnEnviar.setOnClickListener()
you are creating a new instance startActivity(intencio);
instead of returning to the parent activity setResult(RESULT_OK, intencio);finish();
.
try this
btnEnviar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intencio = Activitat1.this.getIntent();
intencio.putExtra(nom, etNom.getText().toString());
intencio.putExtra(cognom, etCognom.getText().toString());
intencio.putExtra(sexe,rbtSexe.getText().toString());
setResult(RESULT_OK, intencio);
finish(); }
});
answered Nov 19 at 16:18
k3b
10.8k54169
10.8k54169
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378427%2fim-not-getting-data-from-another-activity%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown