Android: android.content.res.Resources$NotFoundException: String resource ID #0x5

AndroidStringFile

Android Problem Overview


I get the exception from the title when I run my app. What it does is it has a .txt file with words for a Hangman game and I think the exception is thrown when accessing the file. My file, cuvinte.txt is located into /assets/. Here is my code (i skipped the layout/xml part, which works fine):

public void onCreate() {
    // all the onCreate() stuff, then this:
    try {
        AssetManager am = this.getAssets();
	    InputStream is = am.open("cuvinte.txt");
	    InputStreamReader inputStreamReader = new InputStreamReader(is);
        BufferedReader b = new BufferedReader(inputStreamReader);
	    String rand;
	    while((rand=b.readLine())!=null){
            cuvinte.add(rand);
  	    }
    } catch (IOException e) {
        Toast.makeText(this, "No words file", Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
      
    newGame(newG);
}
    
public void newGame(View view){
  	Random rand = new Random();
   	String stringCuvant = cuvinte.get(rand.nextInt(cuvinte.size()));
   	cuvant.setText("");
   	System.out.println(stringCuvant);
   	for(int i = 0; i< stringCuvant.length(); i++){
   		cuvant.append("_ ");
   	}
   	incercari.setText(valIncercari);
}

The function newGame() is called both when the new game button is pressed and at the beginning of the activity, in the onCreate() function.

Android Solutions


Solution 1 - Android

(Just assumption, less info of Exception stacktrace)

I think, this line, incercari.setText(valIncercari); throws Exception because valIncercari is int

So it should be,

incercari.setText(valIncercari+"");

Or

incercari.setText(Integer.toString(valIncercari));

Solution 2 - Android

Another scenario that can cause this exception is with DataBinding, that is when you use something like this in your layout

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <variable
            name="model"
            type="point.to.your.model"/>
    </data>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@{model.someIntegerVariable}"/>
        
</layout>

Notice that the variable I'm using is an Integer and I'm assigning it to the text field of the TextView. Since the TextView already has a method with signature of setText(int) it will use this method instead of using the setText(String) and cast the value. Thus the TextView thinks of your input number as a resource value which obviously is not valid.

Solution is to cast your int value to string like this

android:text="@{String.valueOf(model.someIntegerVariable)}"

Solution 3 - Android

Just wanted to point out another reason this error can be thrown is if you defined a string resource for one translation of your app but did not provide a default string resource.

Example of the Issue:

As you can see below, I had a string resource for a Spanish string "get_started". It can still be referenced in code, but if the phone is not in Spanish it will have no resource to load and crash when calling getString().

values-es/strings.xml

<string name="get_started">SIGUIENTE</string>

Reference to resource

textView.setText(getString(R.string.get_started)

Logcat:

06-11 11:46:37.835    7007-7007/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.app.test PID: 7007
android.content.res.Resources$NotFoundException: String resource ID #0x7f0700fd
        at android.content.res.Resources.getText(Resources.java:299)
        at android.content.res.Resources.getString(Resources.java:385)
        at com.juvomobileinc.tigousa.ui.signin.SignInFragment$4.onClick(SignInFragment.java:188)
        at android.view.View.performClick(View.java:4780)
        at android.view.View$PerformClick.run(View.java:19866)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Solution to the Issue##

Preventing this is quite simple, just make sure that you always have a default string resource in values/strings.xml so that if the phone is in another language it will always have a resource to fall back to.

values/strings.xml

<string name="get_started">Get Started</string>

values-en/strings.xml

<string name="get_started">Get Started</string>

values-es/strings.xml

<string name="get_started">Siguiente</string>

values-de/strings.xml

<string name="get_started">Ioslegen</string>

Solution 4 - Android

This problem mostly occurs due to the error in setText() method

Solution is simple put your Integer value by converting into string type as

textview.setText(Integer.toString(integer_value));

Solution 5 - Android

Sometime this happened due to not fond any source like if i want to set a text into a textview from adapter then i should use

 textView.setText(""+name);

If you write something like

 textView.setText(name);

this will not work and sometime we don't find the resource from the string.xml file then this type of error occur.

Solution 6 - Android

Using DataBinding and setting background to the EditText with resources from the drawable folder causes the exception.

<EditText
    android:background="@drawable/rectangle"
    android:imeOptions="flagNoExtractUi"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:hint="Enter Your Name"
    android:gravity="center"
    android:textColorHint="@color/hintColor"
    android:singleLine="true"
    android:id="@+id/etName"
    android:inputType="textCapWords"
    android:text="@={viewModel.model.name}"
    android:fontFamily="@font/avenir_roman" />

Solution

I just change the background from android:background="@drawable/rectangle" to android:background="@null"

Clean and Rebuild the Project.

Solution 7 - Android

Sometime we add resources in dimen or any other recourse folder, we forget to add dp.

<dimen name="exercise_value">4</dimen>

For example, I was facing this issue because I forgot to add dp in my value.
Then I changed it into

<dimen name="exercise_value">4dp</dimen> <br>

Boom. Error is gone.

Solution 8 - Android

In order to solve it I had to replace:

Toast.makeText(this, my_int_var, Toast.LENGTH_SHORT).show();

with this:

Toast.makeText(this, String.valueOf(my_int_var), Toast.LENGTH_SHORT).show();

Solution 9 - Android

You are assigning a numeric value to a text field. You have to convert the numeric value to a string with:

String.valueOf(variable)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFloIancuView Question on Stackoverflow
Solution 1 - Androiduser370305View Answer on Stackoverflow
Solution 2 - AndroidKayvan NView Answer on Stackoverflow
Solution 3 - AndroidAndrea ThackerView Answer on Stackoverflow
Solution 4 - AndroidSuyogView Answer on Stackoverflow
Solution 5 - AndroidpavelView Answer on Stackoverflow
Solution 6 - AndroidTandoh Anthony Nwi-AckahView Answer on Stackoverflow
Solution 7 - AndroidShah Nizar BalochView Answer on Stackoverflow
Solution 8 - AndroidE. C. TheodorView Answer on Stackoverflow
Solution 9 - AndroidgdoisView Answer on Stackoverflow