skip to Main Content

Main Activity
`public class MainActivity extends AppCompatActivity {

private Object Context;
public static final String MSG="com.mp.exampleapp.ORDER";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

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);
}

`Second activity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_birthdaygreeting);


     Intent intent = getIntent();
    String msg = "HAPPY BIRTHDAY "+intent.getStringExtra(MainActivity.MSG);
    // receive the value by getStringExtra() method
    // and key must be same which is send by first activity
    TextView textView= (TextView)findViewById(R.id.textView2);
   textView.setText(msg);

}

After running this program – after clicking the button- the message that’s getting displayed is "HAPPY BIRTHDAY null". I don’t know what’s going wrong.

2

Answers


  1. 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);
    }
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search