Passing values through bundle and get its value on

2019-01-25 07:50发布

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
}

10条回答
ら.Afraid
2楼-- · 2019-01-25 08:09
if (getIntent().getExtras().getString("url") != null) {
        // retrieve the url
}

you have to check against null values

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-25 08:10

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");
}
查看更多
Deceive 欺骗
4楼-- · 2019-01-25 08:12

Hi i hope this code helps you.

Bundle bundle = new Bundle();
bundle.putString("name", "Android");
bundle.putString("iname", "iPhone");
Intent intent = new Intent(getApplicationContext(), MyActivity.class);
intent.putExtras(bundle);
startActivity(intent);

In MyActivity.class

public Bundle getBundle = null;
getBundle = this.getIntent().getExtras();
String name = getBundle.getString("name");
String id = getBundle.getString("iname");
查看更多
孤傲高冷的网名
5楼-- · 2019-01-25 08:13

Try adding this inside NotificationService.class:

String url = getIntent().getStringExtra("url");
查看更多
登录 后发表回答