Displaying "Happy birthday null" in the second activity of my app
What is wrong?
The place where you are passing the put extra, is in getIntent()
.This means that you are giving an intent extra to the previous activity.In this case, when you go back, and then get that extra, you will get it.But not on the next activity. Now you get null because that extra does not exist for that activity.So, it comes null.
How to solve?
Instead of using this
public void createBirthdayCard(View view) {
EditText text = (EditText)findViewById(R.id.nameInput);
String str = text.getText().toString();
Toast.makeText(this, "Button was clicked "+str,Toast.LENGTH_LONG).show();
getIntent().putExtra(MSG,str);
startActivity(new Intent(this,birthdaygreeting.class)) ;
ImageView image=(ImageView) findViewById(R.id.cake);
image.setImageResource(R.drawable.wish);
}
use this
public void createBirthdayCard(View view) {
EditText text = (EditText)findViewById(R.id.nameInput);
String str = text.getText().toString();
Toast.makeText(this, "Button was clicked "+str,Toast.LENGTH_LONG).show();
startActivity(new Intent(this,birthdaygreeting.class).putExtra(MSG,str)) ;
ImageView image=(ImageView) findViewById(R.id.cake);
image.setImageResource(R.drawable.wish);
}
How do I know it will help me?
It will help you because here in the new intent which you are giving, there the extra is being given. So, that value is being sent.
Create an intent then add your data in it like the code below.
public void createBirthdayCard(View view) {
EditText text = (EditText)findViewById(R.id.nameInput);
String str = text.getText().toString();
Toast.makeText(this, "Button was clicked "+str,Toast.LENGTH_LONG).show();
Intent intent = new Intent(this,birthdaygreeting.class);
intent.putExtra(MSG,str);
startActivity(intent) ;
ImageView image=(ImageView) findViewById(R.id.cake);
image.setImageResource(R.drawable.wish);
}