SDK 19 及更低版本无法识别 android.support.v7.widget.CardView ,膨胀类<unknown>时出错



在我的Xml文件中我有cardView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingBottom="2dp">
<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:foreground="?android:attr/selectableItemBackground"
    android:id="@+id/channel_cardview"
    app:cardBackgroundColor="@android:color/white"
    app:cardElevation="2dp"
    app:cardMaxElevation="2dp"
    app:cardUseCompatPadding="true">
   .... some other elements here 
  </RelativeLayout>

当我在SDK 23上运行我的应用程序时,它工作正常,但当我在SDK 19或SDK 16上运行此应用程序时,它不工作并抛出此错误:

E/AndroidRuntime: FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #10: Error inflating class <unknown>
    at android.view.LayoutInflater.createView(LayoutInflater.java:613)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    at pb.pocketboard.ChannelFragment$ChannelsAdapter.onCreateViewHolder(ChannelFragment.java:578)
    at pb.pocketboard.ChannelFragment$ChannelsAdapter.onCreateViewHolder(ChannelFragment.java:537)
    at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5482)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4707)

错误指向第一行。10是cardViewCLass,也许系统不识别Cardview类,所以我搜索了相关问题,问题是没有添加依赖项,但我已经添加了它

my Gradle:

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.mikhaellopez:circularimageview:2.1.1'
compile 'com.isseiaoki:simplecropview:1.0.16'
compile 'com.android.support:recyclerview-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'
compile 'com.android.support:support-v4:23.4.0'
compile('com.github.afollestad.material-dialogs:core:0.8.5.3@aar') {
    transitive = true
}
compile files('libs/commons-io-2.4.jar')
}

我的java类,我正在膨胀布局

    @Override
public ChannelsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View rowView = inflater.inflate(R.layout.custom_channel_row, parent, false); // error is here 
    ChannelsViewHolder holder = new ChannelsViewHolder(rowView , this);
    return holder;
    }
你知道为什么会这样吗?

CardView在棒棒糖版本中添加。当您尝试使用RecyclerView(或API 20 (v5.0)或更高版本中添加的任何其他视图)时,也会出现同样的问题。虽然它们是在支持库中添加的

现在,一旦你想到为什么他们被添加到支持库?

这是因为开发者可以在旧版本的android手机中使用它们,而不是因为开发者可以在旧版本的SDK中使用它们。基本上,开发人员总是要在更新的SDK上开发应用程序。

我想这可能就是原因。

进入项目结构,点击add library dependency,选择com.android.support:cardview-v7:23.2.1,按ok,问题就解决了。点击下面的链接了解

https://i.stack.imgur.com/AhYIN.png

https://i.stack.imgur.com/jDCGN.png

最新更新