- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试向我的 dimens.xml
文件中添加一个 float 。
我正在阅读 following SO answer .当我尝试该解决方案时,我得到了评论中描述的异常。我想弄清楚为什么会抛出异常。
为了完整起见,这里是 XML:
<item name="zoom_level" format="float" type="dimen">15.0</item>
这是爆炸的代码:
final float zoom = this.getResources().getDimension(R.dimen.zoom_level);
进入Android源码,这里是getDimension的方法定义:
public float getDimension(int id) throws NotFoundException {
synchronized (mTmpValue) {
TypedValue value = mTmpValue;
getValue(id, value, true);
if (value.type == TypedValue.TYPE_DIMENSION) {
return TypedValue.complexToDimension(value.data, mMetrics);
}
throw new NotFoundException(
"Resource ID #0x" + Integer.toHexString(id) + " type #0x"
+ Integer.toHexString(value.type) + " is not valid");
}
}
因此无论出于何种原因 value.type != TypedValue.TYPE_DIMENSION
。我没有完全设置我的 Android 源代码,所以我不能轻易地在其中添加 Log.w("YARIAN", "value type is "+ value.type)'
语句。
然后我跳入 getValue
调用链似乎是:
Resources.getValue -> AssetManager.getResourceValue -> AssetManager.loadResourceValue
loadResourceValue
是一种 native 方法,这就是我的挖掘失败的地方。
有人知道了解正在发生的事情的最佳方法是什么吗?
我还注意到 Resources
有一个 TypedValue.TYPE_FLOAT
和 TypedValue.TYPE_DIMENSION
。但是在 XML 中,我不能写 type="float"
。
评论中描述的解决方法是使用 type=string
,然后使用 Float.parse
获取 float 。这是必要的吗?为什么或为什么不?
最佳答案
我知道这是一个迟到的答案,但你应该使用 TypedValue#getFloat()而不是像您建议的那样将字符串解析为 float 。
XML:
<item name="float_resource" format="float" type="raw">5.0</item>
Java:
TypedValue out = new TypedValue();
context.getResources().getValue(R.raw.float_resource, out, true);
float floatResource = out.getFloat();
如果您愿意,可以将fraction
、raw
或string
作为type
,这仅对应于R
中的资源类。
关于android - 资源 $NotFoundException : resource ID not valid. 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15650384/
我正在使用 CustomMultiAutoCompleteTextView 引用 this 选择多个联系人在该代码中选择多个联系人,但我想显示在 toast 消息上选择的数字是什么。当为此单击按钮时,
我想改变我的 snackbar 的颜色 snackbarView.setBackgroundColor(ContextCompat.getColor(context, Color.RED)); 我得到
E/AndroidRuntime( 5751): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.exam
我已经用maven构建了我的项目所有构建成功并且 eclipse 属性生成也成功 但是当我在 Eclipse 4 中打开项目时,出现此错误 An internal error occurred dur
我刚刚学习了 Android 编码,这是一个简单的除法、乘法、加法和减法应用程序的源代码,该应用程序在我和我 friend 的 Android 手机上崩溃(在 Lenovo A390 和 Nexus
这是我的 .java 文件: package com.example.rohitkulkarni.readingfile; import android.os.Environment; import
因此,每当我运行我的应用程序时,它都会立即崩溃并给出以下错误: No package identifier when getting value for resource number 0x00000
我正在使用 zxing 解码二维码图像,但它总是返回 NotFoundException。在线解码器 http://zxing.org/w/decode.jspx扫描这些图像非常好,所以它应该能够在我
我注意到我的华为手机生产应用的许多崩溃报告与复数处理完全相关。其他手机没有这个问题只有华为才有。 所有复数形式都存在并且在其他设备上也能正常工作。 看来华为根本无法处理复数: android.cont
我有一个拥有超过 100.000 名用户的应用程序。但是在某些设备(~50)上我得到了一个奇怪的异常。堆栈跟踪显示,未找到可绘制对象。 这是堆栈跟踪: java.lang.RuntimeExcepti
我尝试使用 Zxing 来解码 Aztec 代码。 我使用来自 SO answer 的代码.这是其中的一部分: public static String readQRCode(String fileP
我正在创建一个应用程序,玩家可以在该应用程序中将自己的名字输入到编辑文本中。此信息被检索并通过 extras 发送到第二个 Activity 。每当我运行它时,我都会收到 Resources$NotF
我尝试构建 apk 的发布版本并在 Honor 5x (api 23) 上启动时出现错误: Caused by: android.view.InflateException: Binary XML f
我的一个应用在三星 S5(准确地说是 SM G900V)上崩溃了,但有以下异常(exception): android.content.res.Resources$NotFoundException:
我收到来自 android 市场的崩溃报告: android.content.res.Resources$NotFoundException: Resource ID #0x.... 我每周会收到大约
我有一个动态功能模块,它有自己的嵌套导航图。我还有一个动态功能模块,它只是一个 fragment 。如果您有一个带有自己导航图的动态功能模块,我知道您需要使用 包含动态标签。当我构建应用程序并将应用程
我的选项菜单中有一个麦克风按钮。当我单击它时,我希望它显示备用图像(红色麦克风)。 但是它不起作用。失败并出现 ResourcesNotFoundException。图像(png)肯定位于适当的 re
我正在循环显示近 4,000 张带有图像名称的图像。 这是我用来从可绘制文件夹获取图像的代码 for(int i=0; i
我试图扩展一个RelativeLayout对象并包含一个嵌入的SurfaceView对象,该对象使用/res/drawable文件夹中的png文件作为背景,但我在XML布局编辑器中不断收到错误。请参阅
崩溃发生在不同设备、不同 Android 版本的总用户中的 0.1%。 android.content.res.Resources$NotFoundException: at android.co
我是一名优秀的程序员,十分优秀!