Android资源转换为字符串TypedValue警告

DJC*_*DJC 51 resources android

好的,我正在寻找过去的东西......

每当我在我的应用程序中并且我更改活动时,logcat会报告一系列警告:

02-04 14:42:36.524: WARN/Resources(1832): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f08002b}
02-04 14:42:36.524: WARN/Resources(1832): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f08002c}
02-04 14:42:36.524: WARN/Resources(1832): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f08002d}
Run Code Online (Sandbox Code Playgroud)

其他应用程序没有显示此类警告.这是一个预发布/接受压缩的东西吗?

Tob*_*run 60

仅在启用某个开发人员选项时才会出现这些警告.

设备设置>开发人员选项>禁用"启用视图属性检查"


小智 55

您正在使用bool资源,其中包含字符串.

通过打开生成的R.java文件并从logcat消息中搜索资源ID,可以找到错误使用的资源:

0x7f08002b
0x7f08002c
0x7f08002d
Run Code Online (Sandbox Code Playgroud)

这三个都应该来自您的bool.xml文件(警告消息中的"t = 0x12"表示资源是TYPE_INT_BOOLEAN).

然后,找到项目中正在使用这些资源ID的位置(可能是布局xml,但可以在任何地方),并确保类型匹配.

这是一个生成该日志消息的TextView示例.如果在我的res/values/bool.xml中我有:

<resources>
    <bool name="foo_flag">false</bool>
</resources>
Run Code Online (Sandbox Code Playgroud)

我可以从布局xml文件中错误地引用它:

<TextView android:id="@+id/foo"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@bool/foo_flag"></TextView>
Run Code Online (Sandbox Code Playgroud)

当我运行该应用程序时,我将收到警告消息,因为"text"需要字符串资源,而不是bool(我的应用程序按预期显示,因为标志转换为字符串"false").

  • 谢谢迈克!原来不是bool资源,但检查资源ID是我需要的.我有android:text ="@ id/foo" - 我的TextView文本字段中的整数@id. (5认同)
  • 有趣的是,我的环境(带有MOTODEV的Eclipse)默认TextView文本字段与其id字段具有相同的内容.很高兴看到布局中的ID,但在运行时会产生这些警告.毫无疑问,对于皈依者来说也存在(轻微的)性能损失. (2认同)