How to identify where code is incorrect, if I get

2020-06-18 03:01发布

问题:

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)

回答1:

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.



回答2:

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.



回答3:

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.