如果收到警告 Converting to string: TypedValue, 如何识别代码错误的地方?

发布于 2024-10-30 03:39:08 字数 1128 浏览 6 评论 0原文

以下是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葬シ愛 2024-11-06 03:39:08

从 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 by t)
  • a ==> Additional information about
    where the value came from; only set
    for strings.
  • r ==> eventual resource id (not set
    if you passed a literal value)

So I guess you have to look for integers that you put where it expected strings.

坏尐絯 2024-11-06 03:39:08

这个问题也困扰着我;我发现 logcat 警告来自 android:defaultValue,而不是数组中的 条目。您可以通过在 xml 文件中创建字符串条目(我使用 /xml/constants.xml,但命名约定取决于您并且并不重要)来解决这些消息,如下所示:

 <resources>
      <string name="someValueA">12345</string>
      <string name="someValueB">0</string>
      <string name="someValueC">6789</string>
 </resources>

即使这些值是整数,因为您正在声明它们作为字符串,Android 认为它们是字符串,因此不会生成 logcat 警告。

在您的代码中,无论您需要在何处放置这些值,都可以根据需要引用 @string/someValueAR.string.someValueA(或 B 或 C 等)。对于 xml 文件中的 ListPreference ,您可以使用如下内容:

 <ListPreference
       android:defaultValue="@string/someValueA"
       android:dialogTitle="Some dialog title"
       android:entries="@array/someNamesA"
       android:entryValues="@array/someValuesA"
       android:key="some_preference"
       android:summary="Your summary text"
       android:title="Some Title" />

一旦找到导致问题的条目,解决它并不可怕。将 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:

 <resources>
      <string name="someValueA">12345</string>
      <string name="someValueB">0</string>
      <string name="someValueC">6789</string>
 </resources>

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 or R.string.someValueA (or B, or C, etc.) as appropriate, wherever you need to put those values. In the case of a ListPreference in an xml file, you would use something like this:

 <ListPreference
       android:defaultValue="@string/someValueA"
       android:dialogTitle="Some dialog title"
       android:entries="@array/someNamesA"
       android:entryValues="@array/someValuesA"
       android:key="some_preference"
       android:summary="Your summary text"
       android:title="Some Title" />

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.

小苏打饼 2024-11-06 03:39:08

当我在以下位置启用“启用视图属性检查”选项时遇到了这个问题:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文