I am trying to pass an integer from a fragment to another java class however I keep getting an error: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String, int)' on a null object reference
Bundle b = new Bundle();
b.putInt("id", position);
Intent passInt = new Intent(view.getContext(), displayAccount.class);
passInt.putExtras(b);
startActivity(passInt);
startActivity(new Intent(view.getContext(), displayAccount.class));
Then in displayAccount.java:
int id = (getIntent().getExtras().getInt("id",0));
You called double times startActivity. It will be work when you delete this line startActivity(new Intent(view.getContext(), displayAccount.class));. Because you didn't put bundle on this line, you just starting the activity. You already give intent with startActivity(passInt);
Related
I am trying to transfer some data between two activities in android. I need to transfer an Integer value and a list. When I try and retrieve my values in the second activities they are null values. I'm not sure whether it should be in the onCreate() method
This is the code to send the data.
public void Start(View view) {
Intent goToNextActivity = new Intent(getApplicationContext(), MainActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("topics", (ArrayList<? extends Parcelable>) topicsList);
bundle.putInt("num_players", num_players);
startActivity(goToNextActivity);
}
This is the code to retrieve the data.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle extras = getIntent().getExtras();
num_player = extras.getInt("num_players");
topics_list = extras.getIntegerArrayList("topics");
}
I'm getting a NullPointerException
[ERROR] java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference
What is the correct way to do what I'm trying to do?
you forgot to put your bundle into your intent
goToNextActivity.putExtra("bunlde",bundle)
or you can do it without a bundle ,just put your data into intent
correct code:
Intent goToNextActivity = new Intent(getApplicationContext(), MainActivity.class);
goToNextActivity .putParcelableArrayList("topics", (ArrayList) topicsList);
goToNextActivity .putInt("num_players", num_players);
startActivity(goToNextActivity);
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
My MainActivity.class is invoking putExtra towards SecondActivity.class via setOnClickListener
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("sentString", stringName);
startActivity(intent);
SecondActivity.class setOnClickListener
// Recieve the extra sent from MainActivity.class
Intent SecondActivityIntent = getIntent();
String mString = SecondActivityIntent .getExtras().getString("sentString");
// Send extra to another activity ThirdActivity.class
SecondActivityIntent.putExtra("sentString", mString);
startActivity(SecondActivityIntent);
ThirdActivity.class setOnClickListener
// Recieve extra from SecondActivity.class
Intent thirdActivityintent = getIntent();
String mString = thirdActivityintent.getExtra().getString("sentString");
// This time I am calling SecondActivity.class but I will not send extra
thirdActivityintent = new Intent(ThirdActivity.this, SecondActivity.class);
startActivity(thirdActivityintent);
ThirdActivity.class is causing an
'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
what does this error mean? Is it because SecondActivity.class is expecting to get an extra from any calling activity? I don't intend to putExtra on ThirdActivity or am I force to. How can this be solve?
Is it because SecondActivity.class is expecting to get and extra from any calling activity?
Yes.
How can this be solve?
Put a default value and a null check
// Receive the extra sent from MainActivity.class
Bundle extras = getIntent().getExtras();
String mString = "default";
if (extras != null) {
mString = extras.getString("sentString");
}
Change it into like this
FirstActivity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("sentString", stringName);
startActivity(intent);
In SecondActivity
// Recieve the extra sent from MainActivity.class
Intent SecondActivityIntent = getIntent();
String mString = SecondActivityIntent .getStringExtra("sentString");
// Send extra to another activity ThirdActivity.class
Intent thirdIntent = new Intent(SecondActivity.this, ThirdActivity.class);
thirdIntent.putExtra("sentString", mString);
startActivity(thirdIntent);
The ThirdActivity
// Recieve extra from SecondActivity.class
Intent thirdActivityintent = getIntent();
String mString = thirdActivityintent.getStringExtra("sentString");
// Just finish Activity
finish();
Change
.getExtras().getString("sentString");
to
.getExtras().getString("sentString", "defaultValue");
I must mention i am new to Android and Java. I am trying this for a weeek to solve.
I have Serializable class wich object are populated with http json, and i am using adapters to populate listviews and everything works fine but when i want to pass to another class one object i it force closes, please if somebody can correct my code.
this void is in Serializable class
public void save(){
Intent intent = new Intent();
Bundle extra = intent.getExtras();
intent.putExtra("title", getTitle());
}
when i try this:
Intent intent = new Intent(this, Fragment2.class); i got error The constructor
Intent(FeedItem, Class<Fragment2>) is undefined
and this is fragment class where i want to use passed object
Intent intent = getActivity().getIntent();
Bundle extras = intent.getExtras();
FeedItem feedItem = (FeedItem)getActivity().getIntent().getSerializableExtra("title");
String title = feedItem.getTitle();
Toast.makeText(getActivity(), title, Toast.LENGTH_LONG).show();
error The value of the local variable extras is not used
Many thanks.
Fragments are added like this
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
ft.replace(resourceidwhereyouwantadd(R.id.view), new YourFragment());
ft.commit();
I've seen this thread: Android: how to use data passed from parent activity in a sub activity? and ready about this method, but I'm wondering if this is outdated. Android has made a lot of strides since then and I don't want to do this the wrong way.
Is this still the method that should be used?
This method is perfectly fine and is still used today. There is one more way in which you can pass the data using Bundle.
Bundles can be used as follows
Intent intent = new Intent(ActivityA.this, ActivityB.class);
Bundle b = new Bundle();
b.putString("name", "abcd");
b.putInt("value", 666);
//Add the set of extended data to the intent and start it
intent.putExtras(b);
startActivity(intent);
And in the other activity use
Bundle b = getIntent().getExtras();
int value = b.getInt("value", 0);
String name = b.getString("name");
I am calling an Activity using an Intent, and I need to pass variables to that Activity when is initialized. On iOS you can use a custom initialization using the initWithNibName method. How can a similar thing be achieved on Android?
Here is my code that creates an Intent...
Intent myIntent = new Intent(webPush.this, webPushActivity.class);
startActivity(myIntent);
Intent myIntent = new Intent(webPush.this, webPushActivity.class);
myIntent.putExtra("mystring",strValue)' <<---put String here
startActivity(myIntent);
and in second Activity...
String str = getIntent.getExtras().getString("mystring");<<get string in second class
and check this
How do I pass data between Activities in Android application?
You can put extra data into the Intent...
myIntent.putExtra("exampleString","This is some extra data");
myIntent.putExtra("exampleNumber",1234);
When you call the Intent, it starts the Activity. in one of the main methods of the Activity, like onCreate(), you can access the Intent and get the extras from it, like so...
Intent callingIntent = getIntent();
String exampleString = callingIntent.getStringExtra("exampleString");
int exampleNumber = callingIntent.getIntExtra("exampleNumber");
You can set extras to the intent:
myIntent.putStringExtra("First key", 1);
myIntent.putStringExtra("Second key", "some string");
And then get it in the new activity
Int extraInt = getIntent().getIntExtra("First key");
String extraString = getIntent().getStringExtra("Second key");
See more in the Intent docs
This can be done with intent extras. For example:
int variable = 6;
Intent myIntent = new Intent(webPush.this, webPushActivity.class);
myIntent.PutExtra("stringLabel", variable);
startActivity(myIntent);