简体   繁体   English

Android:android.content.res.Resources$NotFoundException:字符串资源 ID #0x5

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

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.它的作用是它有一个包含 Hangman 游戏文字的 .txt 文件,我认为在访问该文件时会抛出异常。 My file, cuvinte.txt is located into /assets/.我的文件 cuvinte.txt 位于 /assets/。 Here is my code (i skipped the layout/xml part, which works fine):这是我的代码(我跳过了 layout/xml 部分,效果很好):

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. function newGame() 在按下新游戏按钮时和活动开始时在 onCreate() function 中被调用。

(Just assumption, less info of Exception stacktrace) (只是假设,异常堆栈跟踪的信息较少)

I think, this line, incercari.setText(valIncercari);我认为,这一行, incercari.setText(valIncercari); throws Exception because valIncercari is int抛出异常,因为valIncercariint

So it should be,所以应该是

incercari.setText(valIncercari+"");

Or或者

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

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".正如您在下面看到的,我有一个西班牙语字符串“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() .它仍然可以在代码中引用,但如果手机不是西班牙语,它将在调用getString()时没有资源加载和崩溃。

values-es/strings.xml值-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始终有一个默认的字符串资源,这样如果手机使用另一种语言,它就会始终有一个可以回退的资源。

values/strings.xml值/字符串.xml

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

values-en/strings.xml值-en/strings.xml

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

values-es/strings.xml值-es/strings.xml

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

values-de/strings.xml值-de/strings.xml

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

Another scenario that can cause this exception is with DataBinding, that is when you use something like this in your layout可能导致此异常的另一种情况是使用 DataBinding,即当您在布局中使用类似的内容时

<?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.请注意,我使用的变量是一个整数,我将它分配给 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.由于 TextView 已经有一个带有setText(int)签名的方法,它将使用此方法而不是使用setText(String)并转换值。 Thus the TextView thinks of your input number as a resource value which obviously is not valid.因此 TextView 将您的输入数字视为显然无效的资源值。

Solution is to cast your int value to string like this解决方案是将您的 int 值转换为这样的字符串

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

This problem mostly occurs due to the error in setText() method此问题主要是由于setText()方法中的错误引起的

Solution is simple put your Integer value by converting into string type as解决方案很简单,将您的Integer数值转换为string类型作为

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

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.这将不起作用,有时我们没有从 string.xml 文件中找到资源,然后就会发生这种类型的错误。

Using DataBinding and setting background to the EditText with resources from the drawable folder causes the exception.使用 DataBinding 并使用 drawable 文件夹中的资源为EditText设置背景会导致异常。

<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"我只是将背景从android:background="@drawable/rectangle"更改为android:background="@null"

Clean and Rebuild the Project.清理并重建项目。

Sometime we add resources in dimen or any other recourse folder, we forget to add dp.有时我们在 dimen 或任何其他资源文件夹中添加资源,我们忘记添加 dp。

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

For example, I was facing this issue because I forgot to add dp in my value.例如,我面临这个问题是因为我忘记在我的值中添加 dp 。
Then I changed it into然后我把它改成了

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

Boom.繁荣。 Error is gone.错误消失了。

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();

You are assigning a numeric value to a text field.您正在为文本字段分配一个数值。 You have to convert the numeric value to a string with:您必须将数值转换为字符串:

String.valueOf(variable)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android.content.res.resources notfoundexception $ android.content.res.resources notfoundexception $字符串资源ID#0x1 - android.content.res.resources notfoundexception $android.content.res.resources notfoundexception $ string resource id #0x1 android.content.res.Resources $ NotFoundException:资源ID#0x7f0a003b类型#0x5无效 - android.content.res.Resources$NotFoundException: Resource ID #0x7f0a003b type #0x5 is not valid Android:android.content.res.Resources $ NotFoundException:字符串资源ID - Android: android.content.res.Resources$NotFoundException: String resource ID android.content.res.Resources $ NotFoundException:资源ID#0x0 - android.content.res.Resources$NotFoundException: Resource ID #0x0 android.content.res.Resources $ NotFoundException:资源ID#0x0 - android.content.res.Resources$NotFoundException: Resource ID #0x0 android.content.res.Resources$NotFoundException · 字符串资源 ID # - android.content.res.Resources$NotFoundException · String resource ID # android.content.res.Resources$NotFoundException:字符串资源 ID - android.content.res.Resources$NotFoundException: String resource ID GridView.FATAL例外:main android.content.res.Resources $ NotFoundException:字符串资源ID#0x0 - GridView.FATAL EXCEPTION: main android.content.res.Resources$NotFoundException: String resource ID #0x0 android.content.res.Resources $ NotFoundException:字符串资源ID#0x2 - android.content.res.Resources$NotFoundException: String resource ID #0x2 android.content.res.Resources $ NotFoundException:访问getResource()时的字符串数组资源ID#0x0 - android.content.res.Resources$NotFoundException: String array resource ID #0x0 while accessing getResource()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM