Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
449 views
in Technique[技术] by (71.8m points)

android - Passing values through bundle and get its value on another activity

I am passing value through Bundle as you can see in my code.
Now, I want its value in another activity onCreate(). I tried it to get its value but it is showing nullpointerexception.

Please help me solve the problem.

Bundle bundle = new Bundle();
String url = "http://www.google.com";
bundle.putString("url", url);
Intent myIntent = new Intent(context, NotificationService.class);
myIntent.putExtras(bundle);
context.startService(myIntent);


Get Value code :

if (!getIntent().getExtras().getString("url").contains(null)) {
        // Do something
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This should be the procedure.

Create a new Intent with bundle and start the activity.

Intent i = new Intent(context, ActivityName.class);
i.putExtra("key", mystring);
startActivity(i);

Take the bundle like this in new Activity inside onCreate

Bundle extras = getIntent().getExtras();
String value;
if (extras != null) {
  value = extras.getString("key");
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...