android.content.res.Resources $ NotFoundException:字符串资源ID [英] android.content.res.Resources$NotFoundException: String resource ID

查看:152
本文介绍了android.content.res.Resources $ NotFoundException:字符串资源ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用程序抛出异常


android.content.res.Resources $ NotFoundException:String资源ID

android.content.res.Resources$NotFoundException: String resource ID

我的情况并不常见(因为给定的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.

推荐答案

您可能已经发现,当我们尝试将一些整数作为一个字符串设置时,可能会发生此错误:
代码 some_variable 被声明为 int ,然后尝试将其设置为 some_textview code> setText()方法:

You maybe already found that this error happens when we are trying to set some integer as a string like: somewhere in the code some_variable was declared as int and then it tries to set it to some_textview using setText() method:

int some_variable = 0;
TextView some_textview = (TextView) findViewById(R.id.someTextView);
... some code/calculations etc...
some_textview.setText(some_variable); // exception! some_variable is an int value

也可以是任何返回int结果返回的结果,如:

also it could be a result returned by any method which returns int as a result, like:

some_textview.setText(someString.length());// exception! String.length() returns an int value
some_textview.setText(someSet.size());// exception! Set.size() returns an int value

等。

这些导致$ code资源$ NotFoundException cuz没有这样的数字(id)的 String 资源。 R.java 具有静态变量,其中每个 String 都有自己的唯一编号(id,你在代码中使用它R.string ID )。这可能会发生,最终 some_variable 将有一些有效的数字,但在这种情况下,用户将在 some_textview 。

解决这个问题很简单(如果 some_variable 的值必须显示):

these causes the Resources$NotFoundException cuz there is no String resource with such number (id). R.java has static variables where every String has its own unique number (id, and you use it in code as R.string.id). It probably could happen that eventually some_variable will have some valid number but in this case user will see a wrong text string in some_textview.
The solution to this issue is simple (if some_variable's value must be shown):

some_textview.setText(String.valueOf(some_variable));

还有一些人试图设置 int 的价值与 Toast 消息的相同方式,如:

There are also a number of people who are trying to set the int value in the same manner to the Toast message like:

int some_variable = 0;
... some code/calculations etc...
Toast.makeText(this, some_variable, Toast.LENGTH_LONG).show();
Toast.makeText(getBaseContext(), someSet.size(), Toast.LENGTH_LONG).show();

修复程序是一样的:

int some_variable = 0;
... some code/calculations etc...
Toast.makeText(this, String.valueOf(some_variable), Toast.LENGTH_LONG).show();
Toast.makeText(getBaseContext(), String.valueOf(some_Set.size()), Toast.LENGTH_LONG).show();

您可能遇到使用 Integer(some_variable).toString()方法。不算太差。但是这意味着只能使用整数值(或者将抛出 NumberFormatException ),而不是 String.valueOf(some_variable)它无关紧要,它适用于任何类型的值,包括所有原语(float,double,byte,long等),并且永远不会抛出任何异常。

但还有一些其他情况。 这里描述了罕见的情况,重点是清理项目在Eclipse中。

如果您很确定您不会将任何数字传递给接受 String 的方法,此外,应用程序会发生这种情况这是发布(并没有发生在开发)可能是一个情况,当你的应用程序有 Locale 依赖字符串资源。当您测试它时,您可能会使用区域特定的 Locale 设备,因此没有检测到任何问题。但是在这种情况下,您应该提供一个位于项目的 res \values 文件夹中的默认的 strings.xml 。如果你错过了,可能就是这样。如果有些用户可以说德国人默认为 Locale ,则DE试图运行这样的应用程序(只有 strings.xml values-en values-it 等文件夹中,但没有 values-de 文件夹中的strings.xml 将会遇到崩溃资源$ NotFoundException

这个问题的修复就像我之前提到的,提供一个默认的 strings.xml 在项目的 res \values 文件夹中,或者确保(在Application-exteded类中),您的应用程序正在为您的应用程序设置正确的默认区域设置应用程序如果在不支持的区域设置的设备上运行,如:

You maybe met fixes which uses Integer(some_variable).toString() approach. Its not bad. However this means that only integer value could be used (or NumberFormatException will be thrown) than for String.valueOf(some_variable) it doesn't matter cuz it works for any type of value, including all primitives (float, double, byte, long etc) and will never throw any exception.
But there are some other cases. The rare cases are described here and the point is to clean the project in Eclipse.
In case if you are pretty sure you don't pass any numbers to the method which accepts String and moreover this happens with an app which is released (and did not happen while developing) it could be a case when your app has Locale depended String resources. And when you did test it you probably did it using your region specific Locale devices so no issues was detected. However in this case you should provide a default strings.xml located in res\values folder of a project. If you miss it this probably is the case. If some user, lets say, from a Germany who's default Locale is DE tries to run such app (which only has strings.xml in folders like values-en, values-it etc but there is no strings.xml in values nor in values-de folders) will catch a crash with Resources$NotFoundException.
The fix for this issue is, like I mentioned before, providing a default strings.xml in res\values folder of a project or to make sure (in Application-exteded class for instance) your app is setting a proper default locale for your app if its running on a device with unsupported locale, like:

    Locale locale = getAppLocale(context);
    String localeLang = locale.getLanguage();
    // default locale is EN but available are EN and RU
    if (!localeLang.equalsIgnoreCase("en") && !localeLang.equalsIgnoreCase("ru"))
        locale = new Locale("en");
    Configuration config = new Configuration();
    config.locale = locale;

    context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());

这篇关于android.content.res.Resources $ NotFoundException:字符串资源ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆