Passing variable between activities through intent throws NullPointerException - android

Why does this throw NullPointerException ?
In Redeem.java
int yourInt = 200;
Intent myIntent = new Intent(Redeem.this, MainActivity.class);
myIntent.putExtra("intVariableName", yourInt);
startActivity(myIntent);
In MainActivity.java
Bundle extras = getIntent().getExtras();
int score = extras.getInt("intVariableName");

Try with:
Bundle extras = getIntent().getExtras();
String stringScore = extras.getString("intVariableName");
int score = Integer.parseInt(stringScore);
Or:
int score = intent.getIntExtra("intVariableName", 0);

You can also try this:
int yourInt = 200;
Intent myIntent = new Intent(Redeem.this, MainActivity.class);
myIntent.putExtra("intVariableName", String.valueOf(yourInt));
startActivity(myIntent);
In MainActivity.java
Bundle extras = getIntent().getExtras();
int score =Integer.parseInt(extras.getString("intVariableName"));

Use this:
getIntent().getIntExtra("intVariableName", 0); //where 0 is default value

Related

Intent not sending or receiving integer value

public void Show(int n){
Intent in= new Intent(this, Results.class);
in.putExtra("Percent", n); // n is inetger
startActivity(in);`enter code here`
}
Intent intent= getIntent();
// int num = Integer.parseInt(String.valueOf(intent.getStringExtra("Percent")));
String num = intent.getStringExtra("Percent");
int num2= Integer.parseInt(num);
//ch.setText(num);
//String num= in.getStringExtra("Percent");
//int n=
//b.setProgress(num);
t.setText(num2);
You can recieve integer value like this:
Intent intent= getIntent();
int num = intent.getIntExtra("Percent");
You need to use int num intent.getInt("Percent");

Bundle.putInt() , how does it work?

I am still learning android and I in a tutorial
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
int id_To_Search = arg2 + 1;
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", id_To_Search);
Intent intent = new Intent(getApplicationContext(),com.example.addressbook.DisplayContact.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
});
My problem is here I didn't understand how this works
dataBundle.putInt("id", id_To_Search);
if id_To_Search = 0 then id = 0 ? and id refer to the column name ?
android site explains
putInt(String key, int value)
Inserts an int value into the mapping of this Bundle, replacing any existing value for the given key.
as far I understand, if int = 5 then key will be 5 ?
then 'Id' it will be = 5 ?
1) You can share int with other activity like this :
String YOUR_KEY = "id";
Intent intent = new Intent(getApplicationContext(), DisplayContact.class);
intent.putExtra(YOUR_KEY, id);
startActivity(intent);
or
Bundle dataBundle = new Bundle();
dataBundle.putInt(YOUR_KEY, id);
intent.putExtras(dataBundle);
2) And get int in your activity like that :
int defaultValue = 0;
int id = getIntent().getIntExtra(YOUR_KEY, defaultValue);
or
Bundle extras = getIntent().getExtras();
int id = extras.getInt(YOUR_KEY);
That's exaclty what you said
as far I understand, if int = 5 then key will be 5 ? then 'Id' it will be = 5 ?
So ,when you enter in another activity, here
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", id_To_Search);
Intent intent = new Intent(getApplicationContext(),com.example.addressbook.DisplayContact.class);
intent.putExtras(dataBundle);
startActivity(intent);
You can do int id = getIntent().getIntExtra("id"); , and will be 5

passing int into android activities

i pass int to next activity using this code
Intent intent = new Intent(A.this, B.class);
intent.putExtra("selectedType", i);
startActivity(intent);
and then receive this in activity B
Intent intent = new Intent();
int i = intent.getIntExtra("selectedType", 0);
Toast.makeText(getApplicationContext(), String.valueOf(i),
Toast.LENGTH_LONG).show();
but when in this activity, it always display 0.
Intent intent = new Intent();
You are creating a new intent instead of using the one passed to your ActivityB. So use
Intent intent = getIntent();
instead;
use this int i = getIntent().getIntExtra("selectedType", 0);
try getIntent().getExtras().getInt("selectedType")
Try now,
int value = getIntent().getExtras().getInt("selectedType");
Intent intent = new Intent(A.this, B.class);
intent.putExtra("selectedType", i);
startActivity(intent);
Intent intent = new getIntent();
^^^^^^^^^
int i = intent.getIntExtra("selectedType", 0);
Toast.makeText(getApplicationContext(), String.valueOf(i),
Toast.LENGTH_LONG).show();
Because you're creating a new intent and trying to get "selectedType" on it. But that intent has just been created, so it hasn't that value that you seek.
Try to getIntent() method to get your calling intent, which has your "selectedType" value...
Here's a snap:
Bundle extras = getIntent().getExtras();
if(extras != null) {
int value = extras.getIntExtra("selectedType", 0);
Toast.makeText(getApplicationContext(), String.valueOf(value), Toast.LENGTH_LONG).show();
}
and then receive this in activity B
Intent intent = new Intent();
int i = intent.getIntExtra("selectedType", 0);
This is wrong. You are creating a new intent object. To get the intent object that was used to start this activity use getIntent() method.
Intent intent = getIntent();
int i = intent.getIntExtra("seelctedType", 0);
Intent intent = new Intent(A.this, B.class);
intent.putExtra("selectedType",i);
startActivity(intent);
And receiving..
if (getIntent().getExtras().containsKey("selectedType")) {
int message = getIntent().getIntExtra("selectedType");
Toast.makeText(ReceiverActivity.this, "" + message, Toast.LENGTH_LONG)
.show();
}

Getting Null values while Passing datas through Intent to another activity

I am getting null Values while passing datas through Intent from 1 activity to another activity.
Passing from 1Activity:
int s = position;
String str=adapter.getId(position);
int Type=DeviceType;
Bundle bunch=new Bundle();
bunch.putString("id", str);
bunch.putInt("DeviceType",Type);
bunch.putInt("position", s);
Intent It=new Intent();
It.setClass(PYSActivity.this,GridImages.class);
It.putExtras(bunch);
startActivity(It);
Retriveing here in 2 Activity:
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
Bundle b = this.getIntent().getExtras();
AppID=b.getString("id");
DeviceType=b.getInt("DeviceType");
Position=b.getInt("position");
list=(GridView)findViewById(R.id.list);
adapter1=new LazyAdapter1(this,mStrings,like,Rate,Id,img);
list.setAdapter(adapter1);
Do something like this:
Activity 1:
int myInt = 5;
String myString = "hi";
...
Intent Intent = new Intent(...);
intent.putExtra("string_key", myString);
intent.putExtra("int_key", myInt);
startActivity(intent);
Activity 2:
int getInt;
String getString;
...
Bundle extras = getIntent().getExtras();
// Read the extras data if it's available.
if (extras != null)
{
getInt = extras.getInt("int_key");
getString = extras.getString("string_key");
}
Why you are creating bundle just use intent.
it.putExtra("id", str);
it.putExtra("DeviceType",Type);
it.putExtra("position", s);

How to pass integer from one Activity to another?

I would like to pass a new value for an integer from one Activity to another.
i.e.:
Activity B contains an
integer[] pics = { R.drawable.1, R.drawable.2, R.drawable.3}
I would like activity A to pass a new value to activity B:
integer[] pics = { R.drawable.a, R.drawable.b, R.drawable.c}
So that somehow through
private void startSwitcher() {
Intent myIntent = new Intent(A.this, B.class);
startActivity(myIntent);
}
I can set this integer value.
I know this can be done somehow with a bundle, but I am not sure how I could get these values passed from Activity A to Activity B.
It's simple. On the sender side, use Intent.putExtra:
Intent myIntent = new Intent(A.this, B.class);
myIntent.putExtra("intVariableName", intValue);
startActivity(myIntent);
On the receiver side, use Intent.getIntExtra:
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
Their are two methods you can use to pass an integer. One is as shown below.
A.class
Intent myIntent = new Intent(A.this, B.class);
myIntent.putExtra("intVariableName", intValue);
startActivity(myIntent);
B.class
Intent intent = getIntent();
int intValue = intent.getIntExtra("intVariableName", 0);
The other method converts the integer to a string and uses the following code.
A.class
Intent intent = new Intent(A.this, B.class);
Bundle extras = new Bundle();
extras.putString("StringVariableName", intValue + "");
intent.putExtras(extras);
startActivity(intent);
The code above will pass your integer value as a string to class B. On class B, get the string value and convert again as an integer as shown below.
B.class
Bundle extras = getIntent().getExtras();
String stringVariableName = extras.getString("StringVariableName");
int intVariableName = Integer.parseInt(stringVariableName);
In Activity A
private void startSwitcher() {
int yourInt = 200;
Intent myIntent = new Intent(A.this, B.class);
intent.putExtra("yourIntName", yourInt);
startActivity(myIntent);
}
in Activity B
int score = getIntent().getIntExtra("yourIntName", 0);
In Sender Activity Side:
Intent passIntent = new Intent(getApplicationContext(), "ActivityName".class);
passIntent.putExtra("value", integerValue);
startActivity(passIntent);
In Receiver Activity Side:
int receiveValue = getIntent().getIntExtra("value", 0);

Categories

Resources