geetha
geetha

Reputation: 313

Issues with putExtra and getStringExtra

I just started to learn Android application development. I am working out on the example in android tutorial from the site http://developer.android.com/training/basics/firstapp/index.html

While sending the text from my MyActivity page to DiplayMessageActivity,

String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE); 

is returning null as value.

Here is the code for my sendMessage(),

public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText)findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra("EXTRA_MESSAGE",message);
    String message1 = intent.getStringExtra(EXTRA_MESSAGE);
    startActivity(intent);
}

To the extent, I can read about putExtra and getStringExtra() methods, I did, but I could not resolve the issue. I may missing very tiny thing but couldn't trace out.

Can someone help me with this issue?

Upvotes: 0

Views: 1891

Answers (4)

jyomin
jyomin

Reputation: 1967

Instead of String message1 = intent.getStringExtra(EXTRA_MESSAGE); use String message1 = getIntent().getStringExtra(EXTRA_MESSAGE);

Upvotes: 1

For Sending info use like below

public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText)findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra("EXTRA_MESSAGE",message);
startActivity(intent);

}

Get this message in DisplayMessageActivity.class like below

Intent in=getIntent();
String message1 = in.getStringExtra("EXTRA_MESSAGE");

Upvotes: 0

Sagar Devkota
Sagar Devkota

Reputation: 1192

Use like this:

String message1 = intent.getStringExtra("EXTRA_MESSAGE");

here you are using EXTRA_MESSAGE as Constant in getStringExtra and String Literal in putExtra. So either use Literal or as Constant Name.

Upvotes: 2

cnnagpal
cnnagpal

Reputation: 397

check that when you put "EXTRA_MESSAGE" in intent then you are using different constant may be MyActivity.EXTRA_MESSAGE is having different value

Upvotes: -1

Related Questions