Android Resources | NotFoundException feedback from sub-activity [duplicate] - android

Im trying to display a toast message with integer inside it
This is how i tried to do it:
Toast.makeText(this,bignum,Toast.LENGTH_LONG).show();
But it keeps crash my app.
Thanks for help!

Toast.makeText either takes a CharSequence or an int as its second argument.
However, the int represents a resource ID (such as R.string.hello_world).
The application crashes probably because no resource is found with that ID, since it's not an ID to start with, but an arbitrary integer.
In your case, use Toast.makeText(this,String.valueOf(bignum),Toast.LENGTH_LONG).show();.

you need a String
Toast.makeText(this, String.valueOf(bignum),Toast.LENGTH_LONG).show();
otherwise android will try to look it up for a String with id bignum, in your strings.xml file

Try this to "cast" bignum to string:
Toast.makeText(this,"" + bignum,Toast.LENGTH_LONG).show();

You can do this:
Toast.makeText(getBaseContext(), "" + bignum, Toast.LENGTH_LONG).show();

Related

variable/resource concatenation in toast not working

This works:
Toast.makeText(getApplicationContext(), attemptsRemainingCount.toString(), Toast.LENGTH_LONG).show();
This works:
Toast.makeText(getApplicationContext(), R.string.attemptsRemaining, Toast.LENGTH_LONG).show();
This however, does not work:
Toast.makeText(getApplicationContext(), attemptsRemainingCount.toString() + R.string.attemptsRemaining, Toast.LENGTH_LONG).show();
All it gives me is a long number. Could someone please tell me what I'm doing wrong with the concatenation here?
You're concatenating java variable with a Resource Identifier.
Try using following:
Toast.makeText(getApplicationContext(), attemptsRemainingCount.toString() + getResources().getString(R.string.attemptsRemaining), Toast.LENGTH_LONG).show();
Your first example is using a CharSequence which in turn calls the CharSequence version of makeText:
makeText(Context context, CharSequence text, int duration)
And your second example is using the resId or linked resource id version:
makeText(Context context, int resId, int duration)
That explains why the two examples work.
What you want is a combination of both. This means you need to convert the linked resource id to a String, then concatenate it and use the CharSequence method. For example, like so:
Toast.makeText(getApplicationContext(),
attemptsRemainingCount.toString() +
getResources().getString(R.string.attemptsRemaining),
Toast.LENGTH_LONG).show();
The first two lines may look the same, but they will use different overloaded versions of Toast.makeText. The first uses the CharSequence version (because it is given a string) while the second uses the int version (because it is given a integer resource id.
Concatenating the string with a resource id (a number) still results in a string, but with the resource id converted to string.
Instead you should make the string resource into a format string (You have %1$d attempts left) and use:
getResources().getString(R.string.attemptsRemaining, attemptsRemainingCount);
To get the formatted string. See the section Formatting strings in the string resources documentation for full details.
The advantage of format strings in string resources over simply concatenating the number with a fixed string is that the former can easily be translated into other languages where the number may not be at the same place.

TextView shows number instead of character

I want to print ŒHI 5¹ in my TextView. So I have simply written :
tv.setText("Welcome to " + R.string.app_name)
where R.string.app_name is <string name="app_name"><b>ŒHI 5¹</b></string>
But the strange thing is textview is showing a number
The number is: 2131230755
I have no idea why this is happening.Please help.
use getString(R.string.app_name)
or
getResources().getString(R.string.app_name);
R.string.app_name is just a long number genereted to identify that resource. The result of string + long is just that long concatenated to that string. so you need to get the string corresponding to that identifier.
Actually every resource (layout, drawable, array, string) gets an identifier, these are put in the R file. layout identifiers are kept together in an inner class called layout, strings in string and so on.
use :
getString(R.string.app_name);
or if you are not in an activity then
mContext.getResource().getString(R.string.app_name);
You can't call direct R.string for your requirement .
Pass getResource().getString
Returns the string value associated with a particular resource ID
Finally
getResource().getString(R.string.your_string);

Dynamically adding an int to a textView

This may seem like a stupid question, but for some reason i just can't figure it out- i have a few textViews i add text from an Object all the string fields are added fine. but when i try to add a int to the textView it crashes my application.
heres my code
firstLineOfAddress.setText(addline);
town.setText(town2);
county.setText(county2);
postCode.setText(post);
// the three strings up above work fine if i comment the three below out
// ask , current, done are all ints
askingPrice.setText(ask);
currentOffer.setText(current);
doneUpValue.setText(done);
set like this:
textView.setText(10+"");
In your code replace this:
askingPrice.setText(ask);
currentOffer.setText(current);
doneUpValue.setText(done);
with
askingPrice.setText(ask+"");
currentOffer.setText(current+"");
doneUpValue.setText(done+"");
The problem is that you have to convert your int to String. To convert your int to String, use :
Integer.toString(myInt)
or
String.valueOf(myint)
So it would be askingPrice.setText(Integer.toString(myInt))
when you add any Integer number the call this...
textview.setText(""+value);
OR
textview.setText(String.valueOf(value));
where value is a Integer.
You have to set the text value as a String. Convert it as so:
askingPrice.setText(String.valueOf(ask));
currentOffer.setText(String.valueOf(current));
doneUpValue.setText(String.valueOf(done));
try to convert it to a String perhaps .
askingPrice.setText(String.format("%d",ask));
well trying above scenario like this
int text=0;
TextView tv = (TextView)findViewById(R.id.test);
tv.setText(text);
throws exception as
(26955): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.highactivity/com.example.highactivity.MainActivity}: android.content.res.Resources NotFoundException: String resource ID #0x0
because while passing integer in set text it takes as resource id.At run time search for resource id =0 but there wont be any resource id like that so it throws exception will leads to app crashes.convert int to string if you need to display integer

showing a integer number in edit text field in android

I am trying to do the following :
t1 is an edit text
a and b are two integers
t1.setText(a+b);
but this is not working in android but it works perfectly with javaswing
A simple solution would be:
t1.setText("" + (a+b));
or maybe:
t1.setText((a+b).ToString());
please do like the following
EditText et=(EditText)findViewById("ID of your EDITTEXT");
int c=a+b;
String s=c.toString();
et.setText(s);
This'll work fine :) If it works vote me :)
but this is not working in android but it works perfactly with
javaswing
It will not, because you're actually calling setText(int resId). See here. Calling this method will search a string in xml resources (E.g. strings.xml). Every id of a string (E.g. #strings/hello) will be compared to resId, If resId matches the string's id that string will be displayed to that widget or else you wil get a ResourceNotFoundException
To display the actual integer, convert it first to String
t1.setText(String.valueOf(a+b))
You are doing like this t1.setText(a+b); it search this id (a+b) in resource file like strings.xml that is not available. So it will throws a exception ResourceNotFoundException..
So to set the number in text view you need to convert it into string.
In Android you need to do like this:-
int sum = a + b;
String sumString = String.valueOf(sum);
t1.setText(sumString);
OR
t1.setText((a+b) + "");

Android setText / R.string / values

I am having trouble with setting text in a text view with format and multiple values.
holder.car.setText(R.string.mycar + lm.getCarName() + R.string.year + lm.getYear());
this is giving me " 2143545 Camero 2143213 1977 "
I have tried few other "solutions" from the web
holder.car.setText(getString(R.string.mycar) + lm.getCarName() + getString(R.string.year) + lm.getYear()); << not work, getString undefine>>
I even tried String.valueOf(R.string.mycar); getResources().getText(R.String.mycar), still it didn't work.
It would be great if someone can help me, thanks
Try this
holder.car.setText(getResources().getString(R.string.mycar));
I think you're trying to use parameters in your string.
Try this:
<string name="mycar">Car: %1$s Year: %2$s</string>
String mycar = getString(R.string.mycar);
mycar = String.format(mycar, lm.getCarName(), lm.getYear());
You should get:
Car: Camaro Year: 1977
If you want to set your textview just a string from your string.xml file,
mytextview.setText(R.String.mycar);
If you want to set your textview with combination of some strings or integers, (better than first way)
int number=5;
mytextview.setText(getResources().getString(R.String.mycar) + " " +number + " " + getResources().getString(R.String.mysecondcar));
R.string.mycar and R.string.year are only IDs for resources. For this reason you get the numbers (IDs are numeric).
To get string from resources you need to use this construction:
String myCar = getResources().getString(R.string.mycar);
and now the myCar variable holds the string you put in strings.xml file under the mycar name.
the method getResources() belongs to Context. If you run your code outside an Activity, use the context instance to get the string, like this:
String myCar = context.getResources().getString(R.string.mycar);
Try this. If you're fetching string in class without extending Activity Get using your Context
holder.car.setText(context.getResources().getString(R.string.mycar));
If you're extending Activity
holder.car.setText(yourActivity.this.getResources().getString(R.string.mycar));
Hope this helps you..
You have to retrieve your resources first, and the call the medthod getString(int), not getText, has you have put.
So, it should be:
getResources().getString(R.String.mycar);
The R class contains kind of pointers to your ressources, so you can not directly use them, use getResources().getString(), as others say.

Categories

Resources