14

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.

Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
newbie
  • 14,582
  • 31
  • 104
  • 146
  • protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_bucket_item); Bundle bundle = getIntent().getExtras(); String value = bundle.getString("bucketno"); TextView textView = (TextView) findViewById(R.layout.textView1); textView.setTextSize(40); textView.setText(value); } – User Dec 24 '12 at 07:53

7 Answers7

22

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.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • 1
    It should either be `int value` or `String value = String.valueOf(...);`. You're trying to store an `int` in a `String` with this bit of code. ;) – Cat Dec 24 '12 at 06:16
  • @Andro Selva u r right but he must get parseException or classcaste exception but he is getting nullpointer exception ? – Android Killer Dec 24 '12 at 06:17
  • One other thing I didn't think of before--`setText(value)` will fail on an `int` (in this context). `String.valueOf()` is the better way to go here. – Cat Dec 24 '12 at 06:17
  • or you can even you can use phil's solution if you need it to be String itself. Both will work good. – Andro Selva Dec 24 '12 at 06:22
15

You can use :

In first activity ( MainActivity page )

Intent i = new Intent(MainActivity.this,SecondActivity.class); 
i.putExtra("YourValueKey", yourData.getText().toString());

then you can get it from your second activity by :
In second activity ( SecondActivity page )

Intent intent = getIntent();
String YourtransferredData = intent.getExtras().getString("YourValueKey");
Agilanbu
  • 2,747
  • 2
  • 28
  • 33
Arash GM
  • 10,316
  • 6
  • 58
  • 76
4
String value = Integer.toString(getIntent().getExtras().getInt("bucketno"));
Phil
  • 35,852
  • 23
  • 123
  • 164
2
Bundle extras = getIntent().getExtras();

if (extras != null) { 
    String data = intent.getExtras().getString("key").toString();
}
deHaar
  • 17,687
  • 10
  • 38
  • 51
Srishti Roy
  • 576
  • 5
  • 17
1

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");
Rohan Prasad
  • 151
  • 1
  • 3
0

Use:

String value = getIntent().getExtras().get("key").toString();

getIntent().getExtras() will give you Boundle. get() method can be used to fetch the values.

superhero
  • 6,281
  • 11
  • 59
  • 91
0

In addition who has different sittuation

  double userDMH = Util.getInstance(TakeInfoOne.this).calculateBMH(userGender, kg, talll, currentAge, activityy);
                        Intent intentTogeneral = new Intent(TakeInfoOne.this, TakeInfoTwo.class);
                        intentTogeneral.putExtra("user_current_age",userDMH );
                        startActivity(intentTogeneral);

If you put other primitives like double , boolean , int just dont forget to type correct format while geting the value in secont Activity

Bundle extras = getIntent().getExtras();
    if (extras != null) {
        double value =  extras.getDouble("user_current_age");
        txt_metabolism.setText(""+value);
    }

Here with extras.getDouble() type your concerned value type like

extras.getInt("user_current_age");
extras.getBoolean("user_current_age");
 extras.getString("user_current_age");
Samir
  • 6,405
  • 5
  • 39
  • 42