Application throws an exception
android.content.res.Resources$NotFoundException: String resource ID
My case wasn't that common (since given ID does exist). I asked this question to answer it by myself.
Please see below my answer why could this happen and why it happend for me.
You maybe already found that this error happens when you are trying to set some integer as a string like: somewhere in the code
some_variable
was declared asint
and you want to show its value so you are trying to set it tosome_textview
usingsetText()
method:also it could be a result returned by any method which returns int as a result, like:
etc.
these causes the
Resources$NotFoundException
cuz there is noString
resource with such number (id).As you most probably know at every build time there is a
R.java
file being created in build folder. This file contains static variables where every declared in string.xmlString
has its own unique id with some int value and this id you use in code as R.string.id. This is the reason of exception - system will try to find R.string.id string corresponding to given value but obviously fails in most cases. Also this is a reason why setText() accepts integer values and IDE don't warn you. In some cases it could happen that yoursome_variable
by chance will have a value which corresponds to some id. These are tricky cases because no exception will be thrown and you will see some unexpected text (actually corresponding to R.string.id) instead of variable value.The solution to this issue is simple (if
some_variable
's value must be shown):There are also a number of people who are trying to set the
int
value in the same manner to theToast
message like:The fix is the same:
You maybe met approach which uses
Integer(some_variable).toString()
method. Its not bad. However this means 1 extra operation (wrapping int value to Integer) will be applied and that only integer value could be used (orNumberFormatException
will be thrown) while forString.valueOf(some_variable)
value type doesn't matter, it works for any type of value, including all primitives (float, double, byte, long etc) and will never throw any exception.Also there are some other cases - rare cases are described here and the point is to clean the project in IDE.
In case if you are pretty sure you don't pass any integers in a manner described above and moreover this happens with released app and did not happen while developing it could be a
Locale
case. If your app hasLocale
dependingString
resources and when you tested it you probably did it using your region specificLocale
device(s) and thus no issues was detected. However (bear in mind!) in such case you must provide a defaultstrings.xml
located inres\values
folder of a project and if you missed it this is the case. Assume some user, lets say, from a Germany (who's device defaultLocale
is DE) tries to run such app which only hasstrings.xml
in foldersvalues-en
andvalues-it
(but there is nostrings.xml
invalues
nor invalues-de
folders). In this case app will try to locatevalues-de\strings.xml
first but fails, then it will try to use defaultvalues\strings.xml
and fails once again so it will crash throwingResources$NotFoundException
.The fix for this issue is, like I mentioned before, to provide a default
strings.xml
inres\values
folder of a project so for any unsupported locale the default one will be used (usually english). Another way is to set a proper default locale for your app at run time (in Application extended class for instance) if it runs on a device with unsupported locale, like (assume you want to useen
locale by default):