0

I have a problem with data transfer to other activities there is my code.

...
Intent i = new Intent(MainActivity.this, LoggedMain.class);
Intent toOther = new Intent(MainActivity.this, PickerFormActivity.class);
toOther.putExtra(LOGIN_NAME, userName);
toOther.putExtra(PASSWORD, password);                   
i.putExtra(LOGIN_NAME, userName);
startActivity(i);

in LoggedMain activity i can get value using

Intent i = getIntent();
String userName = i.getExtras().getString(MainActivity.LOGIN_NAME); 

but when i use in PickerFormActivity activity im getting null pointer exception

final String username = getIntent().getExtras().getString(MainActivity.LOGIN_NAME);
final String password = getIntent().getExtras().getString(MainActivity.PASSWORD);

someone can help me with that problem?

  • 2
    use `startActivity(toOther);` for starting Activity because you are adding values in `toOther`intent instead of `i` – ρяσѕρєя K Dec 13 '13 at 14:21
  • i need that values to both activities – user3099680 Dec 13 '13 at 14:23
  • but at a time you can start only one Activity so use switch case or if-else ladder for starting Activity according to condition – ρяσѕρєя K Dec 13 '13 at 14:24
  • i understand that but i need that values to PickerFormAcitivy.class any other method to set values in PickerFormActivity class? I can try to send values to LoggedMain then from logged main i can putExtras to PickerFormAcitivity. I need something like in java setters and getters – user3099680 Dec 13 '13 at 14:25
  • then instead of using Intent for sharing data between both Activities use `SharedPreferences` by which you can access values in both Activities – ρяσѕρєя K Dec 13 '13 at 14:30
  • thank u if someone have same problem there is link http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values – user3099680 Dec 13 '13 at 14:31

3 Answers3

0

Instead of using String mString = getIntent().getExtras().getString(string);

use String mString = (String) getIntent().getSerializableExtra(string);

Batuhan Coşkun
  • 2,961
  • 2
  • 31
  • 48
0

You should start the activity PickerFormActivity with the intent toOther. The data travels "with" the intent created. So if you start the activity with another intent, you will lose the data.

For instance, if you start PickerFormActivity from LoggedMain, you could do:

Intent i = new Intent(MainActivity.this, LoggedMain.class);
i.putExtra(LOGIN_NAME, userName);
i.putExtra(PASSWORD, password);                   
i.putExtra(LOGIN_NAME, userName);
startActivity(i);

Then

Intent i = getIntent();
String userName = i.getExtras().getString(MainActivity.LOGIN_NAME); 
String password = i.getExtras().getString(MainActivity.LOGIN_NAME); 
Intent other = new Intent(LoggedMain.this, PickerFormActivity.class);
other.putExtra(LOGIN_NAME, userName);
other.putExtra(PASSWORD, password); 
startActivity(other);
brent
  • 91
  • 3
0

You are declaring extras for Intent toOther but you are not calling that intent. If you are calling Intent toOther after LoggedMain.class you need to pass all extras you want for Intent toOther trough LoggedMain and pass them on again to next intent. You can only declare in First Activity what to pass to Second Activity. Best thing to do in that case is to use SharedPreferences

djordje6
  • 106
  • 2