Passing values through bundle and get its value on

2019-01-25 08:12发布

问题:

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
}

回答1:

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");
}


回答2:

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");


回答3:

if (getIntent().getExtras().getString("url") != null) {
        // retrieve the url
}

you have to check against null values



回答4:

//put value in intent like this

    Intent in = new Intent(MainActivity.this, Booked.class);
    in.putExtra("filter", "Booked");
    startActivity(in);

// get value from bundle like this

    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();
    String filter = bundle.getString("filter");


回答5:

try this

 String url = "http://www.google.com";

Intent myIntent = new Intent(context, NotificationService.class);
myIntent.putExtras("url", url);
startActivity(myIntent);

and on another activity get it like

Bundle extra = getIntent().getExtras();
if(extra != null) {
String value = extra.getString("url")
//use this value where ever you want

  }


回答6:

Because you put your strings inside a bundle

Bundle youtbundle = new Bundle();
String url = "http://www.google.com";
yourbundle.putString("url", url);

And then put the bundle as an extra to the intent

Intent myIntent = new Intent(context, NotificationService.class);
myIntent.putExtras(yourbundle);
context.startService(myIntent);

In the begining You have to strip the bundle from extras

Bundle yourbundle = getIntent().getExtras().getBundle("yourbundle") ;

Then get Strings from yourbundle

if (!yourbundle.getString("url").contains(null)) {
     // Do something
}


回答7:

Replace startService(...) - > startActivity(...)

And Also replace

if (getIntent().getExtras().getString("url").contains(null)) {
        // retrieve the url
}

TO

if (getIntent().getExtras().getString("url") != null) {
        // retrieve the url
}


回答8:

String value = bundle.getString("request");



回答9:

  1. Intent serviceIntent = new Intent(YourService.class.getName()); serviceIntent.putExtra("url", "www.google.com"); context.startService(serviceIntent);

    1. When the service is started its onStartCommand() method will be called so in this method you can fetch the value (url) from the intent object

    2. public int onStartCommand (Intent intent, int flags, int startId){

    String userID = intent.getStringExtra("url");

    return START_STICKY; }



回答10:

Try adding this inside NotificationService.class:

String url = getIntent().getStringExtra("url");