NumberFormatException when passing variable through Android Intent - java

I'm trying to pass 2 variables through a couple of Android Activities. One of them keeps turning up as null on the last page:
The first Activity:
Intent intent= new Intent(RoundOptionActivity.this, MoveOptionActivity.class);
intent.putExtra("numRounds", "5");
startActivity(intent);
The second Activity:
Bundle extras = getIntent().getExtras();
if(extras !=null) {
numRounds = Integer.parseInt(extras.getString("numRounds"));
}
.........
Intent intent = new Intent(MoveOptionActivity.this, MoveActivity.class);
intent.putExtra("numRounds", numRounds);
intent.putExtra("playerChoice", playerChoice);
startActivity(intent);
(Note that at this point I printed numRounds in LogCat and it was set to the right number, and not null)
The Third Activity:
Bundle extras = getIntent().getExtras();
if(extras !=null) {
playerChoice = Integer.parseInt(extras.getString("playerChoice"));
numRounds = Integer.parseInt(extras.getString("numRounds"));
}
At this point, the application crashes at the line where I try to parse numRounds to an integer, with a NumberFormatException complaining that it can't parse a null value. There's never a problem with playerChoice, only numRounds. I've tried handling numRounds the exact same way as playerChoice, but nothing seems to work. What's going on? D:

You have to use extras.getInt("numRounds");
because in second Activity you added to putExtra int value:
numRounds = Integer.parseInt(extras.getString("numRounds"));

use
numRounds = extras.getInt("numRounds");
intead of
numRounds = Integer.parseInt(extras.getString("numRounds"));
because you are passing numRounds as Integer in intent.putExtra("numRounds", numRounds); from second Activity
or pass as if you want to receive as String:
Intent intent = new Intent(MoveOptionActivity.this, MoveActivity.class);
intent.putExtra("numRounds", numRounds+"");
intent.putExtra("playerChoice", playerChoice);
startActivity(intent);

As far as i think in your second activity you are setting numRounds a integer value in putExtra() i.e the integer variable numRounds thats why it causing problem. either get the numRounds in third activity as directly like extras.getInt("numRounds") or send value as String in second activity i.eintent.putExtra("numRounds", numRounds+"");

Related

Pull String Array Dynamically with name

I want to use an intent string extra to dynamically determine what String array in an XML file to use.
Java code:
Intent myIntent = getIntent();
String stringID = myIntent.getStringExtra("stringID");//pull string id
String[] allStrings = getResources().getStringArray(stringID);
The XML:
<string-array name="set1">
<item>item1</item>
<item>item2</item>
<item>item3</item>
</string-array>
The last line in the java code doesn't work because it wants something like r.array.set1, but I want to choose this dynamically instead. How can I accomplish this? Would it be easier to use the ID of the string array somehow?
Actually r.array.set1 is a reference to an Integer. So you should pass it as an Integer, not a String. So:
Intent myIntent = getIntent();
int intID = myIntent.getIntExtra("stringID", r.array.set1);//The second parameter is the default value if nothing is specified
String[] allStrings = getResources().getStringArray(intID);
And in the other activity pass it as an Integer:
intent.putExtra("stringID", r.array.set1);

How to add multiple numbers from Strings then save them

I am making a simple budget app and would like to add all inputted income then save this income to use in other classes. I'm lost and not sure how to do it. Here's the portion of my code inside of my onCreate method. I have income and incomeName both as Strings
addIncomeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//TODO: Transfer this info to line in scroll view showing incomes
if (TextUtils.isEmpty(enterIncomeEditText.getText()) |
TextUtils.isEmpty(enterIncomeNamesEditText.getText())) {
Toast.makeText(Income.this, "Entry Empty", Toast.LENGTH_SHORT).show();
} else {
//create income and incomeName strings
income = enterIncomeEditText.getText().toString();
incomeName = enterIncomeNamesEditText.getText().toString();
mLayout.addView( createNewTextView(incomeName + " " + income));
}
}
});
You can use Intent.
Assume you want to pass all inputted data to Activity B
Intent intent = new Intent(getApplication(), ActivityB.class);
intent.putExtra("income",income);
intent.putExtra("incomeName",incomeName);
startActivity(intent);
Then in Activity B, you can use getStringExtra() to get all inputted value.

How to add values from MainActivity to list in SecondActivity

I have a problem with adding values to list which is in SecondActivity. In MainActivity I set text in EditText boxes and send to second class. For the first time values are adding, but when I back to previous activity and one more time set text and send it, values in the list are replaced, not added. Someone know what is the source of this problem?
try this in first activity:
String[] array = {"Hi", "there", "yeah"};
Intent goIntent = new Intent(this, NewAppActivity.class);
/*
* put extra with "array" as a key and the String[] with your values as the value to pass
* */
goIntent.putExtra("array", array);
startActivity(goIntent);
and in second activity:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String[] array = extras.getStringArray("array");
}

App crashes when displaying casted int with Toast

I have an Android App calling a PHP script, which returns 0 (FALSE) or 1 (TRUE) - unfortunately as a string. So in my Java Code I have the variable String result which is either "0" or "1". I know (thanks to you guys here) that this string can start with the BOM, so I remove it, if it does.
It is not really necessary but let's say I'd feel better if I had that result as an integer not as a string. The casting from string to int named code seems to work. At least I do not see anything happen.
But when I want to use the casted int like if (code == 1) or display it via Toast, my app crashes.
I can show with Toast that result == "1" and that result.length() == 1 so I do not see how I can possibly fail casting this string to int:
String result = postData("some stuff I send to PHP");
if (result.codePointAt(0) == 65279) // which is the BOM
{result = result.substring(1, result.length());}
int code = Integer.parseInt(result); // <- does not crash here...
// but here...
Toast.makeText(ListView_Confirmation.this, code, Toast.LENGTH_LONG).show();
I also tried using valueOf() and adding .toString() but it just keeps crashing. What am I missing here?
Use the following way to show toast
Toast.makeText(ListView_Confirmation.this, ""+code, Toast.LENGTH_LONG).show();
Toast.makeText requires a String(CharSequence) or an int but this int represents the resource id of the string resource to use (ex: R.string.app_name)
Try instead :
Toast.makeText(ListView_Confirmation.this, String.valueOf(code), Toast.LENGTH_LONG).show();
Use the below
Toast.makeText(ListView_Confirmation.this, String.valueOf(code), Toast.LENGTH_LONG).show();
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#valueOf(int)
public static Toast makeText (Context context, CharSequence text, int duration)
Make a standard toast that just contains a text view.
Parameters
context The context to use. Usually your Application or Activity object.
text The text to show. Can be formatted text.
duration How long to display the message. Either LENGTH_SHORT or LENGTH_LONG
http://developer.android.com/reference/android/widget/Toast.html
So use String valueOf(code) as the second parameter to makeText(params)
Returns the string representation of the integer (code) argument.
According to Toast library Toast.makeText(Context, int, int) uses that integer as Resource ID to Find a String in your resources.
Now since you don't have a resource with the same integer the function throws Resources.NotFoundException .
So to display your int value you have to change it back to text.
Toast.makeText(ListView_Confirmation.this, String.valueOf(code), Toast.LENGTH_LONG).show();
In android Api, the constructor of a toast is :
Toast.makeText(Context context, int resId, int duration)
"resId" is the reference of your String but not a String Object:
example: resId= R.string.helloworld

Passing Value from string.xml from one activity to another?

The value in string.xml file...
<string name="bangla_history_2ndpoint">SOME VALUE </string>
From this activity i am trying to pass value to another activity ... by using putextra
Intent ptwo=new Intent("com.powergroupbd.victoryday.of.bangladesh.HISTORYDESCRIPTION");
ptwo.putExtra("header", R.string.bangla_history_2ndpoint);
startActivity(ptwo);
Then get the value in this activity ...
But it is not getting the value from the string.xml file...
text_point = getIntent().getStringExtra("header");
Toast.makeText(getApplicationContext(), text_point, Toast.LENGTH_LONG).show();
But it is toasting blank ....
Please give a solution...
That's because you're trying to retrieve a String, but what you're passing in as extra is actually the resource identifier of it, an int. Either put an actual string as extra, or retrieve an int on the receiving end to fix this.
// put:
ptwo.putExtra("header", R.string.bangla_history_2ndpoint);
// get:
int extraResourceId = getIntent().getIntExtra("header");
text_point = getString(extraResourceId);
Or:
// put:
ptwo.putExtra("header", getString(R.string.bangla_history_2ndpoint));
// get:
text_point = getIntent().getStringExtra("header");

Categories

Resources