Android< sdk 21给出了InflateException

时间:2015-11-13 19:30:26

标签: android android-compatibility

我使用状态列表drawable来选择和取消选择RecyclerView中的项目行,但它会在<< 21台设备。

风格:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/my_color</item>
    <item name="colorPrimaryDark">@color/my_color</item>
    <item name="colorAccent">@color/my_color</item>
  </style>

我的项目布局:

<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="68dp"
    android:background="@drawable/item_sound"
    android:orientation="horizontal">

  </LinearLayout>

Selector drawable drawable/item_sound.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@color/my_color" android:state_selected="true" />
  <item android:drawable="?android:attr/selectableItemBackground" android:state_selected="false" />
</selector>

例外:

11-13 15:13:09.533 11391-11391/? E/AndroidRuntime: FATAL EXCEPTION: main
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime: Process: com.test.app.android, PID: 11391
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.view.LayoutInflater.createView(LayoutInflater.java:620)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at com.test.app.android.logged.top.TopSongsAdapter.onCreateViewHolder(TopSongsAdapter.java:28)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at com.test.app.android.logged.top.TopSongsAdapter.onCreateViewHolder(TopSongsAdapter.java:17)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5223)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4449)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4359)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.view.View.layout(View.java:14818)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.view.View.layout(View.java:14818)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:581)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.view.View.layout(View.java:14818)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1037)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:747)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:761)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.view.View.layout(View.java:14818)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.view.View.layout(View.java:14818)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.view.View.layout(View.java:14818)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at com.sothree.slidinguppanel.SlidingUpPanelLayout.onLayout(SlidingUpPanelLayout.java:860)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.view.View.layout(View.java:14818)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.view.View.layout(View.java:14818)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1043)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.view.View.layout(View.java:14818)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.view.View.layout(View.java:14818)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.view.View.layout(View.java:14818)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-13 15:13:09.533 11391-11391/? E/AndroidRuntime:  at android.widget.FrameLayo

我认为这与<item android:drawable="?android:attr/selectableItemBackground" android:state_selected="false" /> 有关,但我无法找到原因

为什么?

--------编辑-----------

已经尝试使用"?attr/selectableItemBackground""?android:attr/selectableItemBackground",但没有。

2 个答案:

答案 0 :(得分:0)

look at this answer

似乎无法使用android:drawableandroid:src

的主题属性

答案 1 :(得分:0)

我最终为v21创建了一个drawable,而另一个用于&lt; 21。

attributes in selectors didn't exist prior to API 21. At all. You can certainly create two different selectors, one in drawable and one in drawable-v21