How to identify where code is incorrect, if I get warning Converting to string: TypedValue?

左心房为你撑大大i 提交于 2019-12-03 14:37:23

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!