Is there any way to check if an extra has been passed when starting an Activity?
I would like to do something like (on the onCreate() in the Activity):
Bundle extras = getIntent().getExtras();
String extraStr = extras.getString("extra");
if (extraStr == null) {
extraStr = "extra not set";
}
But this is throwing a java.lang.NullPointerException.
Thank you.
Use the Intent.hasExtra(String name) to check if an extra with name was passed in the intent.
Example:
Intent intent = getIntent();
if (intent.hasExtra("bookUrl")) {
bookUrl = b.getString("bookUrl");
} else {
// Do something else
}
Also, use Intent.getStringExtra(String name) directly on the intent to handle the NullPointerException if no extras were passed.
Well, I had similiar problem. in my case the null point exception was happen when I checked if my bundle.getString() was equall to null.
here is how IN MY CASE I solved it:
Intent intent = getIntent();
if(intent.hasExtra("nomeUsuario")){
bd = getIntent().getExtras();
if(!bd.getString("nomeUsuario").equals(null)){
nomeUsuario = bd.getString("nomeUsuario");
}
}
if (this.getIntent().getExtras() != null && this.getIntent().getExtras().containsKey("yourKey")) {
// intent is not null and your key is not null
}
I think you need to check when extras != null
Bundle extras = getIntent().getExtras();
if (extras != null) {
String extraStr = extras.getString("extra");
}else {
extraStr = "extra not set";
}
I would use this solution in your case.
String extraStr;
try {
extraStr = getIntent().getExtras().getString("extra");
} catch (NullPointerException e ) {
extraStr = "something_else";
}
Working Code
If you want to check first Intent have no extra
Intent intent = getIntent();
if (intent.getExtras() == null){
startActivity(new Intent(Splash.this, Main.class));
overridePendingTransition(R.anim.enter, R.anim.exit);
finish();
}else {
if (intent.hasExtra("type")) {
String type = getIntent().getStringExtra("type");
switch (type){
case "showRateUsDialog":
Intent i = new Intent(Splash.this, Main.class);
i.putExtra("type", "showRateUsDialog");
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
overridePendingTransition(R.anim.enter, R.anim.exit);
finish();
break;
case "refer":
Intent i2 = new Intent(Splash.this, Refer.class);
i2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i2);
overridePendingTransition(R.anim.enter, R.anim.exit);
finish();
break;
default:
startActivity(new Intent(Splash.this, Main.class));
overridePendingTransition(R.anim.enter, R.anim.exit);
finish();
}
}
}
}
Related
Please help this is now driving me crazy.
I have an if, else if and else statement inside a try block.
if (loginSuccess.equalsIgnoreCase("true")) {
emailAddressEditText.setText("");
passwordEditText.setText("");
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(LoginActivity.this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("redirectURL", redirectUrl);
editor.apply();
Intent intent = new Intent(LoginActivity.this, WebviewActivity.class);
intent.putExtra("url", redirectUrl);
startActivity(intent);
} else if (loginSuccess.equalsIgnoreCase("false")) {
Intent intent = new Intent(LoginActivity.this, ErrorActivity.class);
intent.putExtra("errormessage", "loginerror");
startActivity(intent);
} else if (loginSuccess.equalsIgnoreCase("")){
Intent intent = new Intent(LoginActivity.this, ErrorActivity.class);
intent.putExtra("errormessage", "servererror");
startActivity(intent);
}
the first "if" statement never gets executed.
I even tried to create a new String with "true" in it but no success.
Even the log says that the loginSuccess String IS TRUE!
I/System.out: true
What is wrong?
First you have to check for null,
if(loginSuccess != null)
{
if(loginSuccess.toString().contains("true"))
{
// its true
// do your code here as required
}
else{
// not contains true
}
}else
{
// loginSuccess is null
}
Try using
loginSuccess.trim().equalsIgnoreCase("true")
So I'm new to programming. I have a string array named values that has about 150 strings in it. Instead of using a ton of if statements I wan't to to use a for loop that each time it cycles through the loop increments to the next element in the array. I'm sure it's a super simple fix but I just can't solve it. Thanks for any advice!
routeListView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String route = values[position];
int i;
for (i=0; i < values.length;i++) {
if (route.equals(values[0])) {
Intent intent = new Intent(view.getContext(), RouteDetails.class);
intent.putExtra("route", routeDetail[0]);
startActivity(intent);
}
values++;
}
/*if (route.equals(values[0])) {
Intent intent = new Intent(view.getContext(), RouteDetails.class);
intent.putExtra("route", routeDetail[1]);
startActivity(intent);
}
if (route.equals("Main Wall")) {
Intent intent = new Intent(view.getContext(), RouteDetails.class);
intent.putExtra("route", "Map of Main Wall");
startActivity(intent);
}
if (route.equals("1. Shark Bait - 5.9")) {
Intent intent = new Intent(MainActivity.this, RouteDetails.class);
intent.putExtra("route", "Shark Bait");
startActivity(intent);
}
*/
}
Inside of the loop, replace the hard coded 0's with "i". This will allow your code to be ran for each iteration of the loop. For example, the i will be replaced with 0, then 1, then 2, etc.
for (int i=0; i<values.length; i++) {
if (route.equals(values[i])) {
Intent intent = new Intent(view.getContext(), RouteDetails.class);
intent.putExtra("route", routeDetail[i]);
startActivity(intent);
}
}
Also, there is no need to add a counter for values at the end, since it is handled by the i++ in the for loop. Hope that helps!
Looks like you can use switches...if you need both int and string comparisons you can use two switches to do that.
String route = values[position];
switch(position) {
case 0:
Intent intent = new Intent(MainActivity.this, RouteDetails.class);
intent.putExtra("route", routeDetail[0]);
startActivity(intent);
return;
case 1:
// Do stuff
return;
}
switch(route) {
case "Main Wall":
Intent intent = new Intent(MainActivity.this, RouteDetails.class);
intent.putExtra("route", "Map of Main Wall");
startActivity(intent);
return;
case "Shark Bait":
Intent intent = new Intent(MainActivity.this, RouteDetails.class);
intent.putExtra("route", "Shark Bait");
startActivity(intent);
return;
}
I am trying to make a login activity which proceeds to another activity by clicking. but its giving me error. here is the code....
public void onButtonClick(View v) {
if(v.getid() == R.id.Blogin)
{
EditText a=(EditText) findViewById(R.id.TFUserName);
String str= a.getText().toString();
Intent i = new Intent(this, SignIn.class);
i.putExtra("Username",str);
startActivity(i);
}
}
its giving me error in view and getid.. any one can help me in solving this error?
Try by this code .
First Activity:-
Intent i = new Intent(FirstScreen.this, SecondScreen.class);
String keyIdentifer = null;
i.putExtra("STRING_I_NEED", strName);
Second Activity :-
String newString;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("STRING_I_NEED");
}
} else {
newString= (String)savedInstanceState.getSerializable("STRING_I_NEED");
}
complete code ..
or
you can use shared Preference to save values of user Login.
Hi there I want to call an Activity an decide which is the next activity to start after the Dialog finished like:
Intent dialog_intent = new Intent(_parent, MyActivityA.class);
dialog_intent.putExtra(MyActivityA.EXTRA_PARENT, MyActivityB.class);
I am getting the extra like this in the Oncreate of MyActivityA:
Type _parent = null; // this is a class variable
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if (bundle != null) {
Set<String> keys = bundle.keySet();
Iterator<String> it = keys.iterator();
while (it.hasNext())
{
String key = it.next();
if(key.equals(EXTRA_PARENT))
_parent = (Type)bundle.get(EXTRA_PARENT);
}
}
in the finishDialog method I do this :
public void finishDialog(View v)
{
try
{
Intent intent = null;
if(_parent != null && _parent instanceof Activity)
{
intent = new Intent(this, _parent.getClass());
}
else
{
intent = new Intent(this, DefaultActivity.class);
}
if(intent != null)
{
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
finish();
}
2 Questions:
Why does _parent instaneof Activity not work. I think MyActivity is a Type which inherits of Activity
Trying to start the activity does not work. But if i put in
new Intent(this,MyActivityB.class);
it works! What am I doing wrong. Is there any other way to do this
You can simply pass integer value and map it to corresponding activity.
Intent dialog_intent = new Intent(_parent, MyActivityA.class);
dialog_intent.putExtra("EXTRA_PARENT", 1);
Now in your activity get this EXTRA_PARENT and call activity
Bundle extras = getIntent().getExtras();
int extraParent = extras.getInt("EXTRA_PARENT");
if(extraParent == 1)
{
Intent dialog_intent = new Intent(_parent, MyActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
}
in own service i want to send data with intent but i am getting null from Activity.I have read about document from this solution but i can not resolve the problem.
SERVICE :
public void notifyTest(int unread){
Intent i = new Intent();
i.setClass(this, YourDialog.class);
i.putExtra("data", unread);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity ( i );
}
get data from Activity:
Intent intent = getIntent ();
String count = intent.getStringExtra("data");
i can not use BroadCostReceiver to resolve problem.
I think this error happened because your data is int, but you want to receive it as String.
Try to change it like this :
public void notifyTest(int unread){
Intent i = new Intent();
i.setClass(this, YourDialog.class);
i.putExtra("data", unread + ""); //convert to STRING
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity ( i );
}
Try this way,hope this will help you to solve your problem.
unread is int then :
getIntent().getIntExtra("data",0);
unread is String then :
getIntent().getStringExtra("data");
Use this to "put"
Intent i = new Intent(FirstScreen.this, SecondScreen.class);
String keyIdentifer = null;
i.putExtra("STRING_I_NEED", strName);
Then, to retrieve the value try something like:
String newString;
if (savedInstanceState == null) {
extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("STRING_I_NEED");
}
} else {
newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
}