Reputation: 137
when i execute the code shown below
int random = (int)Math.ceil(Math.random()*100);
Toast.makeText(getApplicationContext(), random, Toast.LENGTH_SHORT).show();
i get this log
E/AndroidRuntime( 994): java.lang.RuntimeException: Unable to start activity Co
mponentInfo{com.p/com.p.main}: android.content.res.Resources$NotFoundException:
String resource ID #0x4b
Could you please tell me what is the error?
Upvotes: 0
Views: 450
Reputation: 12636
If you want just display random number:
Toast.makeText(getApplicationContext(), ""+random, Toast.LENGTH_SHORT).show();
If you want to display one of predefined strings just put them into array next do something like that:
String[] myRandomTexts = this.getResources().getStringArray(R.array.myStrings);
int random = (int)Math.ceil(Math.random()*myRandomTexts.size());
Toast.makeText(getApplicationContext(), random, Toast.LENGTH_SHORT).show();
Upvotes: 1
Reputation: 1366
Use this:
Toast.makeText(getApplicationContext(), Integer.toString(random), Toast.LENGTH_SHORT).show();
The Toast (or even TextViews) does not accept integers as input resources, you have to provide string resources.
Upvotes: 2
Reputation: 30855
well you need to type casting from integer to string
try this
int random = (int)Math.ceil(Math.random()*100);
Toast.makeText(getApplicationContext(), ""+random, Toast.LENGTH_SHORT).show();
Upvotes: 1
Reputation: 436
The error you got appears to be unrelated to your code above, as it was caused before the above code was run. Since it's a resource not found exception for a string, I would check your layout files and such to make sure that you aren't using strings that were not created in your strings.xml.
Upvotes: 0
Reputation: 12006
As I understand it: you've programmed your app to generate a random ID value then request the resource (i.e. a text string from the appropriate data XML files). This has very little chance of actually working unless you have a sufficient number of resources at your disposal: 0x4b == 75, so in this case it requests string with an id of 75, which you probably didn't define and hence the crash.
Yep, see also the Android documentation: http://developer.android.com/reference/android/widget/Toast.html#makeText(android.content.Context, int, int)
Upvotes: 4