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");
Related
intent.putExtra() and intent.getStringExtra() are not working. getStringExtra returns null
At sending side:
Intent intent = new Intent(getApplicationContext(), ChooseUserActivity.class);
startActivity(intent);
intent.putExtra("Name",imageName);
At receiving side:
Intent secondIntent = getIntent();
String nameImage = "Something " + secondIntent.getStringExtra("Name");
Log.i("Name of the Image: ",nameImage);
The output of the Log cat:
You should first put Extras in Intent and then start activity not after it
//create intent
Intent intent = new Intent(getApplicationContext(),ChooseUserActivity.class);
//put extras
intent.putExtra("Name",imageName);
//start activity
startActivity(intent);
The problem I am having is that it prints out Null on the second activity and not the actual username that is entered. Is the data being passed to the second activity correctly? Does the second activity need more code? Sorry but not the best at programming.
I have this code in my main class
if (username.getText().toString().equals("batman") &&
password.getText().toString().equals("Joker")) {
Toast.makeText(MainActivity.this, "Username and
password is correct", Toast.LENGTH_SHORT).show();
Intent intent = new Intent("com.example.*******.loginpage.User");
intent.putExtra("username",String.valueOf(username));
startActivity(new Intent(MainActivity.this, User.class));
This is the code inside my second class.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
Intent intent = getIntent();
String username = getIntent().getStringExtra("username");
TextView textView = (TextView) findViewById(R.id.textView4);
textView.setText("Welcome" + " " + username );
The problem is your intent in your first class
Intent intent = new Intent("com.example.*******.loginpage.User"); <-- have created an intent
intent.putExtra("username",String.valueOf(username));
startActivity(new Intent(MainActivity.this, User.class)); <-- but using new Intent
You have created an intent but you passing new intent. Use your created Intent instead of passing new Intent.
Intent intent = new Intent(MainActivity.this, User.class);
intent.putExtra("username",String.valueOf(username));
startActivity(intent);
EDIT
Instead using String.valueOf(username) you must use username.getText(), because String.valueOf(username) is method to translate your object to String.
Intent intent = new Intent(MainActivity.this, User.class);
intent.putExtra("username",username.getText());
startActivity(intent);
Two problems here.
First one is that you have to pass the intent where you put your extra instead of creating new one to startActivity, like
Intent intent = new Intent(MainActivity.this, User.class);
intent.putExtra("username",username.getText().toString());
startActivity(intent);
Second problem is that username looks like editText, String.valueOf won't pass actual value, use username.getText().toString() like i mentioned in code.
I know how to show text with a button click on the same page, but my question is (since I couldn't find anything on Google): Is it possible when you click a button, that the text shows up on another activity?
Yes, you can
In your FirstActivity execute this when button is clicked:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("data","Messsage to be sent");
startActivity(intent);
In your SecondActivity inside onCreate():
String someData = getIntent().getStringExtra("data");
yourTextView.setText(someData);
If you need pass values between the activities you use this:
String name="aaaa";
Intent intent=new Intent(Main_Activity.this,Other_Activity.class);
intent.putExtra("name", name);
startActivity(intent);
And this code to recovery data on new Activity:
Bundle b = new Bundle();
b = getIntent().getExtras();
String name = b.getString("name");
You can use Intent for this. Intent is used to move on other activity from first activity.
You can use :
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("YourValueKey", yourData.getText().toString());
then you can get it from your second activity by :
Intent intent = getIntent();
String YourtransferredData = intent.getExtras().getString("YourValueKey");
This question already has answers here:
Android: Clear the back stack
(39 answers)
Android: Remove all the previous activities from the back stack
(18 answers)
Closed 9 years ago.
For example,
I have activity A, B, C, D
A call B
Intent intent = new Intent(A,B.class);
startActivity(intent);
Then, B call C
Intent intent = new Intent(B,C.class);
startActivity(intent);
After that, C call D
Intent intent = new Intent(C,D.class);
startActivity(intent);
In Activity D, I call finish(). It will return back to Activity C.
My question is how can I clear Activity A, B, C before calling finish() so that the app quit like normal.
Don't suggest call finish() on every startactivity because the app can press back to previous activity to continue.
This should work definitely...
Intent intent = new Intent(D,A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("close",true);
startActivity(intent);
and in oncreat of A activity u have to write
if (getIntent().getBooleanExtra("close", false)) {finish();
}
else {
{
//ur previous code here
}
Have fun if any problem u can ask
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_SINGLE_TOP
FLAG_ACTIVITY_CLEAR_TASK
FLAG_ACTIVITY_NEW_TASK
which ensures that if an instance is already running and is not top then anything on top of it will be cleared and it will be used, instead of starting a new instance (this useful once you've gone Activity A -> Activity B and then you want to get back to A from B, but the extra flags shouldn't affect your case above).
Try adding FLAG_ACTIVITY_NEW_TASK.
So your code would be:
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I am using the following in my application. Hope it will help.
Intent intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // this will clear the stacks
intent.putExtra("exitme", true); // tell Activity A to exit right away
startActivity(intent);
and in Activity A add the following:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
if( getIntent().getBooleanExtra("exitme", false)){
finish();
return;
}
}
try with Intent.FLAG_ACTIVITY_CLEAR_TOP
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
see here
http://developer.android.com/reference/android/content/Intent.html
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);