1

I'm getting a null value returned from getStringExtra at 1 of 3 occurrences. I see the value is present in the debugger with the correct key.

This is how I'm setting my extras:

Intent intent = new Intent(SettingsActivity.this, MainActivity.class);
intent.putExtra("citySearch", city.getText());
intent.putExtra("nDays", nDays.getSelectedItem().toString());
intent.putExtra("method", method.getSelectedItem().toString());
SettingsActivity.this.startActivity(intent);

And this is where I'm asking it back

Intent intent = getIntent();
String search = intent.getStringExtra("citySearch") == null ? "Maribor" : intent.getStringExtra("citySearch");
String nDays = intent.getStringExtra("nDays") == null ? "5" : intent.getStringExtra("nDays");
String method = intent.getStringExtra("method") == null ? "json" : intent.getStringExtra("method");

It's working for the "nDays" and the "method" but somehow not for the "citySearch". The value is always "Maribor", but I've debugged it and the value returned from the .getStringExtra() is null.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
MerceneX
  • 119
  • 1
  • 8
  • city is a textview? If yes, try using city.getText().toString(), otherwise please tell what it is or post some code from where is the info coming. – Ricardo A. Nov 13 '19 at 15:07
  • It's an EditView. And yes, the "toString()" solved my problems. Thanks so much! :D – MerceneX Nov 13 '19 at 15:14

3 Answers3

3

Try changing city.getText() to city.getText().toString(). Hopefully it should work.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Ishita Singh
  • 297
  • 1
  • 6
1
i.putExtra("citySearch", city.getText().toString());

Hope this help.

GHH
  • 1,713
  • 1
  • 8
  • 21
1

Change i.putExtra("citySearch", city.getText()); to i.putExtra("citySearch", city.getText().toString());