Jobin Tensing T
Jobin Tensing T

Reputation: 75

Passing String from one class to another in Android

I want to transfer a String value from one class to another class in same package. The second class is called using intent in onclick of a menu item. I used the code

Intent i = new Intent(TouristGuideActivity.this, PointOfInterest.class);
i.putExtra("videoId", videoId);
startActivity(i);

in first class and then in second class,

String address=getIntent().getExtras().getString("videoId");`

But when I click on the menu item, I get a force close. If I remove that put Extra part, it works ine. But in that case I can't send the string. Please help!

Upvotes: 1

Views: 5916

Answers (2)

Raghav Chopra
Raghav Chopra

Reputation: 527

Intent intent=new Intent(this,secondclass.class);
intent.putExtra("videoId",videoId);
startActivity(intent);

and in your second class try 2 get these items by doing

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

        }

just after u define your class i hope dis would help u out 2 get the values on other page

Upvotes: 4

gwvatieri
gwvatieri

Reputation: 5183

Make sure that you are getting the same type, for example, if you put a String, get a String. Then try with:

getIntent().getStringExtra("videoId");

Upvotes: 1

Related Questions