19

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
}
Jeeten Parmar
  • 253
  • 2
  • 3
  • 11
  • http://stackoverflow.com/questions/15859445/how-do-you-pass-a-string-from-one-activity-to-another/15859488#15859488. check this – Raghunandan Jun 10 '13 at 10:59

10 Answers10

31

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");
}
Naveen T P
  • 6,955
  • 2
  • 22
  • 29
Sreedev
  • 6,563
  • 5
  • 43
  • 66
25

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");
Eng. Samer T
  • 6,465
  • 6
  • 36
  • 43
Bebin T.N
  • 2,539
  • 1
  • 24
  • 28
2
if (getIntent().getExtras().getString("url") != null) {
        // retrieve the url
}

you have to check against null values

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
1

//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");
Savita Sharma
  • 344
  • 3
  • 9
0

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

  }
Avinash Kumar Pankaj
  • 1,700
  • 2
  • 18
  • 27
0

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
}
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
0

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
}
Hamlet Kraskian
  • 683
  • 9
  • 11
0

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

AMIT
  • 390
  • 1
  • 4
  • 17
0
  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; }

Azahar
  • 386
  • 5
  • 14
0

Try adding this inside NotificationService.class:

String url = getIntent().getStringExtra("url");
Oscar Duarte
  • 521
  • 5
  • 5