Android:android.content.res.Resources$NotFoundException:字符串资源 ID #0x5
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11189545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: android.content.res.Resources$NotFoundException: String resource ID #0x5
提问by FloIancu
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):
当我运行我的应用程序时,我从标题中得到了异常。它的作用是它有一个 .txt 文件,其中包含 Hangman 游戏的文字,我认为在访问该文件时会引发异常。我的文件 cuvinte.txt 位于 /assets/ 中。这是我的代码(我跳过了布局/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.
newGame() 函数在按下新游戏按钮时和活动开始时在 onCreate() 函数中调用。
回答by user370305
(Just assumption, less info of Exception stacktrace)
(只是假设,异常堆栈跟踪的信息较少)
I think, this line, incercari.setText(valIncercari);
throws Exception
because valIncercari
is int
我认为,这一行incercari.setText(valIncercari);
抛出异常,因为valIncercari
是int
So it should be,
所以应该是
incercari.setText(valIncercari+"");
Or
或者
incercari.setText(Integer.toString(valIncercari));
回答by David Argyle Thacker
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()
.
正如您在下面看到的,我有一个西班牙语字符串“get_started”的字符串资源。它仍然可以在代码中引用,但如果电话不是西班牙语,它将在调用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.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>
回答by Kayvan N
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. 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.
请注意,我使用的变量是一个整数,我将它分配给 TextView 的文本字段。由于 TextView 已经有一个带有签名的方法,setText(int)
它将使用此方法而不是使用setText(String)
和 强制转换值。因此 TextView 将您的输入数字视为显然无效的资源值。
Solution is to cast your int value to string like this
解决方案是将您的 int 值转换为这样的字符串
android:text="@{String.valueOf(model.someIntegerVariable)}"
回答by Suyog
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));
回答by pavel
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 文件中找到资源,然后就会发生这种类型的错误。
回答by A.N.T
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.
清理并重建项目。
回答by gdois
You are assigning a numeric value to a text field. You have to convert the numeric value to a string with:
您正在为文本字段分配一个数值。您必须将数值转换为字符串:
String.valueOf(variable)