如果收到警告 Converting to string: TypedValue, 如何识别代码错误的地方?
以下是 LogCat 的摘录:
04-04 19:51:51.270: INFO/ActivityManager(57): Starting activity: Intent { cmp=com.example.app/.Preferences }
04-04 19:51:51.710: WARN/Resources(1081): Converting to string: TypedValue{t=0x10/d=0x0 a=-1}
04-04 19:51:51.740: WARN/Resources(1081): Converting to string: TypedValue{t=0x10/d=0x0 a=-1}
04-04 19:51:51.761: WARN/Resources(1081): Converting to string: TypedValue{t=0x10/d=0x79e a=-1}
04-04 19:51:51.800: WARN/Resources(1081): Converting to string: TypedValue{t=0x10/d=0x5a0 a=-1}
04-04 19:51:51.810: WARN/Resources(1081): Converting to string: TypedValue{t=0x10/d=0x5 a=-1}
04-04 19:51:51.830: WARN/Resources(1081): Converting to string: TypedValue{t=0x10/d=0xa a=-1}
04-04 19:51:51.840: WARN/Resources(1081): Converting to string: TypedValue{t=0x10/d=0xa a=-1}
04-04 19:51:51.860: WARN/Resources(1081): Converting to string: TypedValue{t=0x10/d=0x1e a=-1}
04-04 19:51:51.870: WARN/Resources(1081): Converting to string: TypedValue{t=0x10/d=0x1e a=-1}
04-04 19:51:53.450: INFO/ActivityManager(57): Displayed activity com.example.app/.Preferences: 2061 ms (total 2061 ms)
Here is the extract from LogCat:
04-04 19:51:51.270: INFO/ActivityManager(57): Starting activity: Intent { cmp=com.example.app/.Preferences }
04-04 19:51:51.710: WARN/Resources(1081): Converting to string: TypedValue{t=0x10/d=0x0 a=-1}
04-04 19:51:51.740: WARN/Resources(1081): Converting to string: TypedValue{t=0x10/d=0x0 a=-1}
04-04 19:51:51.761: WARN/Resources(1081): Converting to string: TypedValue{t=0x10/d=0x79e a=-1}
04-04 19:51:51.800: WARN/Resources(1081): Converting to string: TypedValue{t=0x10/d=0x5a0 a=-1}
04-04 19:51:51.810: WARN/Resources(1081): Converting to string: TypedValue{t=0x10/d=0x5 a=-1}
04-04 19:51:51.830: WARN/Resources(1081): Converting to string: TypedValue{t=0x10/d=0xa a=-1}
04-04 19:51:51.840: WARN/Resources(1081): Converting to string: TypedValue{t=0x10/d=0xa a=-1}
04-04 19:51:51.860: WARN/Resources(1081): Converting to string: TypedValue{t=0x10/d=0x1e a=-1}
04-04 19:51:51.870: WARN/Resources(1081): Converting to string: TypedValue{t=0x10/d=0x1e a=-1}
04-04 19:51:53.450: INFO/ActivityManager(57): Displayed activity com.example.app/.Preferences: 2061 ms (total 2061 ms)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从 logcat 获取的
TypedValue
可以是解释如下:t
==>类型 (0x10
=TYPE_INT_DEC
)d
==>实际数据(由t
指定解释)a
==>有关的附加信息价值从何而来;只设置
对于字符串。
r
==>最终资源ID(未设置如果你传递了一个文字值)
所以我想你必须寻找放置在预期字符串位置的整数。
The
TypedValue
you get from logcat can be interpreted this way:t
==> type (0x10
=TYPE_INT_DEC
)d
==> the actual data (to be intepreted as specified byt
)a
==> Additional information aboutwhere the value came from; only set
for strings.
r
==> eventual resource id (not setif you passed a literal value)
So I guess you have to look for integers that you put where it expected strings.
这个问题也困扰着我;我发现 logcat 警告来自
android:defaultValue
,而不是数组中的
条目。您可以通过在 xml 文件中创建字符串条目(我使用 /xml/constants.xml,但命名约定取决于您并且并不重要)来解决这些消息,如下所示:即使这些值是整数,因为您正在声明它们作为字符串,Android 认为它们是字符串,因此不会生成 logcat 警告。
在您的代码中,无论您需要在何处放置这些值,都可以根据需要引用
@string/someValueA
或R.string.someValueA
(或 B 或 C 等)。对于 xml 文件中的ListPreference
,您可以使用如下内容:一旦找到导致问题的条目,解决它并不可怕。将 logcat 消息中的“d”值从十六进制转换为十进制应该会为您指明正确的方向。例如,0x5a0 是 1440,因此您应该能够识别代码中何处使用了值 1440。
This issue was bothering me as well; I discovered that the logcat warnings are coming from
android:defaultValue
, not the<item>
entries in the array. You can resolve these messages by creating string entries in an xml file (I use /xml/constants.xml, but the naming convention is up to you and does not matter) as follows:Even though those values are integers, since you are declaring them as strings, Android considers them strings so no logcat warning is generated.
In your code, reference
@string/someValueA
orR.string.someValueA
(or B, or C, etc.) as appropriate, wherever you need to put those values. In the case of aListPreference
in an xml file, you would use something like this:Once you find the entries that are causing the problem, it's not terrible to resolve it. Converting the "d" values in the logcat messages from hex to decimal should point you in the right direction. For example, 0x5a0 is 1440, so you should be able to identify where you used the value 1440 in your code.
当我在以下位置启用“启用视图属性检查”选项时遇到了这个问题:
Settings >开发者选项>调试
一旦我关闭该设置,设备就停止向 logcat 发送这些警告。
I had this problem when I enabled the "Enable View Attribute Inspection" option within:
Settings > Developer Options > Debugging
Once I turned that setting off, the device stopped spamming logcat with these warnings.