1

Main activity:

Intent intent = new Intent(Main.this, Secondary.class);

intent.putExtra("name",value);

startActivity(intent);

Secondary activity:

String value = getIntent().getStringExtra("name")

What's wrong here? I've searched a lot without success...

Thanks

user222
  • 191
  • 2
  • 17
  • Are you sure that getIntent() itself is not returning NULL? – jarmod Jul 28 '13 at 19:51
  • Ok, getIntent() is returning NULL. How to fix that? – user222 Jul 28 '13 at 20:05
  • I am guessing that there is no valid intent at the point that you are calling getIntent(). Are you calling this before onCreate()? See http://stackoverflow.com/questions/13983728/android-using-getintent-only-within-oncreate – jarmod Jul 28 '13 at 20:10
  • Show us where you set the value of value string variable in Main Activity – Peshal Jul 28 '13 at 20:12
  • So I can't use Intent in onCreate? It's called in setOnClickListener when the user click the view. Is that the problem? – user222 Jul 28 '13 at 20:25
  • No that should not be a problem. Check the answer I provided. Everything should work fine. – mipreamble Jul 28 '13 at 20:32

6 Answers6

3

Try this:

In the MainActivity:

//Make sure Secondary is an Activity name. Secondary.class.

Intent intent = new Intent(MainActivity.this, Secondary.class);
intent.putExtra("name",value);
startActivity(intent);

In the Secondary Activity:

String value = getIntent().getExtras().getString("name");

You need to get the bundle first and then extract the string from it.

Bundle bundle = getIntent().getExtras();
if (bundle != null) {
    bundle.getString("name");
}

Both should work. The second one is to check if the bundle is null.

craigcaulfield
  • 3,381
  • 10
  • 32
  • 40
mipreamble
  • 1,415
  • 12
  • 17
  • Initializing variables should be mandatory... or all variables which are not initialized should have a value of 0 in case of string types, and empty in case of uri etc... its just unnecessary (to my primitive mind) as to why this extra work is required... is it for security, because it does not look like its for performane – DragonFire Jan 22 '20 at 12:33
0

When you call putExtra(...), make sure the value object is a String. If you're passing any other object, make sure to explicitly call value.toString(), especially if dealing with GUI components.

See here for more information: Android Intent.getStringExtra() returns null

Community
  • 1
  • 1
Kon
  • 10,702
  • 6
  • 41
  • 58
0

I have used this method times. Just make sure value has a value or initialized.You can use Log or System.out.println(value); after .putExtra to see(in console tab) if value is null. and in second activity too.

MDEVLP
  • 124
  • 1
  • 3
  • 12
0

Change this line

String value = getIntent().getStringExtra("name");

TO this line

String value = getIntent().getString("name");
Peshal
  • 1,508
  • 1
  • 12
  • 22
0

I have discovered I have an issue in my code it was my fault. It wasn't an Intent problem. Thank you all.

user222
  • 191
  • 2
  • 17
0

You can try this -->

intent.putExtra("name", textView.getText().toString());

If the error still occurs, check-in the Second Activity that you are using the right id path...:-

value = findViewById(R.id.);