Android Resources converting to string TypedValue

2019-01-22 10:51发布

Ok I'm looking right past something here..

Every time I'm in my app and I change activities, logcat reports series of warnings:

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}

Other apps are not showing such warnings. Is this a pre-release/aapt compression thing?

5条回答
家丑人穷心不美
2楼-- · 2019-01-22 11:14

You are using a bool resource where a string is expected.

You can find which resource is being used incorrectly by opening your generated R.java file and searching for the resource IDs from the logcat message:

0x7f08002b
0x7f08002c
0x7f08002d

All three should be from your bool.xml file (the "t=0x12" in the warning message means the resources are TYPE_INT_BOOLEAN).

Then, find where those resource IDs are being used in your project (probably a layout xml, but could be anywhere) and make sure the types match.

Here's an example of a TextView that would generate that log message. If in my res/values/bool.xml I have:

<resources>
    <bool name="foo_flag">false</bool>
</resources>

I can incorrectly refer to it from a a layout xml file:

<TextView android:id="@+id/foo"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@bool/foo_flag"></TextView>

When I run that app, I'll get the warning message since "text" expects a string resource, not a bool (my app appears as expected though since the flag is converted to the string "false").

查看更多
姐就是有狂的资本
3楼-- · 2019-01-22 11:17

Check to see if you don't have:-

<TextView android:text="@+id/labelText"/>

in your resource file.

查看更多
冷血范
4楼-- · 2019-01-22 11:18

I've discovered that this warning is also outputted when specifying a plurals string from a widget that requires parameters.

For instance:

<plurals name="song_count">
    <item quantity="one">%d song in playlist</item>
    <item quantity="other">%d songs in playlist</item>
</plurals>

The warning will appear when inflating an activity that contains a widget referencing it:

<TextView
    android:id="@+id/tv_total_songs"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@plurals/song_count" />

You no doubt replace the string after inflating the views to set the correct parameters, e.g.:

playlistSongCount.setText(
        getResources().getQuantityString(
            R.plurals.song_count,
            songCount,
            songCount));

The obvious solution here is to remove the android:text attribute from the layout as it has no purpose.

查看更多
神经病院院长
5楼-- · 2019-01-22 11:33

These warnings only occurred when a certain developer option was enabled.

Device Settings > Developer options > Disable 'Enable view attribute inspection'

查看更多
仙女界的扛把子
6楼-- · 2019-01-22 11:33

Problem in android:text="@+id/fooText

Try change in your .xml this:

<TextView 
    android:id="@+id/foo" 
    android:text="@+id/fooText"/>

To this:

<TextView 
    android:id="@+id/foo" 
    android:text=""/>
查看更多
登录 后发表回答