Reputation: 7012
In main activity i send the String[] name like this:
private void sendNames() {
Bundle b=new Bundle();
b.putStringArray("key", names);
Intent i=new Intent(this, ListFriendsFragment.class);
i.putExtras(b);
}
when i send names it is not empty 100%, and put this code in a method, and called it after the i get the names.
In activity i want to receive the string[] i get it like this:
names = this.getIntent().getExtras().getStringArray("key");
In both, main activity and the one i want to receive the string, names
is declared as follows:
private String[] names;
When i start the activity which should get the names
the application crashes:
Caused by: java.lang.NullPointerException
at com.utm.course.friendslist.ListFriendsFragment.PrintNames(ListFriendsFragment.java:26)
at com.utm.course.friendslist.ListFriendsFragment.onCreate(ListFriendsFragment.java:20)
What am i doing wrong?
Update
these are parts where i use Intent
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (currentSession != null) {
currentSession.onActivityResult(this, requestCode, resultCode, data);
}
}
...
private void sendNames() {
Log.d("sendNames", "started");
Bundle b=new Bundle();
b.putStringArray(key, names);
Intent i=new Intent(this, ListFriendsFragment.class);
i.putExtras(b);
}
...
private void listFriends() {
Log.d("Activity", "List Friends Activity Starting");
Intent i=new Intent(MainActivity.this,ListFriendsFragment.class);
startActivity(i);
finish();
}
Upvotes: 1
Views: 132
Reputation: 2865
Sending
Bundle b=new Bundle();
b.putStringArray(key, new String[]{value1, value2});
Intent i=new Intent(context, Class);
i.putExtras(b);
Receiving
Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray(key);
Upvotes: 0
Reputation: 25267
Just do this way:
Assumption, String[] names;
Intent intent = new Intent(this, ListFriendsFragment.class);
intent.putExtra("key", names);
startActivity(intent);
On next activity,
Intent intent = getIntent();
String[] names = intent.getStringArrayExtra("key");
Upvotes: 0
Reputation: 53535
It looks like sendNames()
does not return the intent you created and you probably call startActivity(i);
somewhere else where the intent you created here is no longer in the scope.
Change the signature of sendNames()
to return the intent you've created and use that intent when you start the activity.
If you'll run with a debugger, add a break point where you start the activity and make sure that the intent you pass is containing that bundle with the "key" string array.
Upvotes: 3