Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass value using Intent between Activity in Android?

Tags:

android

I want to pass the value of the position in one activity class to another...

My code is as follows:

protected void onListItemClick(ListView listView, View v, int position,
            long id) {
        switch (position) {
            case 1:
                Intent newActivity1 = new Intent(this, BucketItemActivity.class);
                newActivity1.putExtra("bucketno", position);
                startActivity(newActivity1);
                break;
            case 2:
                Intent newActivity2 = new Intent(this, BucketItemActivity.class);
                newActivity2.putExtra("bucketno", position);
                startActivity(newActivity2);
                break;
    }
}

The activity class that will receive it..

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bucket_item);
        String value = getIntent().getExtras().getString("bucketno");
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(value);
        setContentView(textView);
    }

But i always get a null in the String value...

Please help.

like image 804
newbie Avatar asked Dec 24 '12 06:12

newbie


2 Answers

From the First Activity

Intent i=new Intent(getApplicationContext(),BookPractionerAppoinment.class);
i.putExtra("prac","test");
startActivity(i);

Getting values in Second ACT Activity

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

For Serialized objects:

Passing

Intent i=new Intent(getApplicationContext(),BookPractionerAppoinment.class);
 i.putExtra("prac",pract);
 startActivity(i);

Getting

pract= (Payload) getIntent().getSerializableExtra("prac");
like image 155
Rohan Prasad Avatar answered Oct 10 '22 21:10

Rohan Prasad


Replace this,

String value = getIntent().getExtras().getString("bucketno");

with

int value = getIntent().getExtras().getInt("bucketno");

You are trying to pass int value but retrieving String Data. That's why you are getting the nullpointerException.

like image 37
Andro Selva Avatar answered Oct 10 '22 21:10

Andro Selva