SDK 19及以下版本无法识别android.support.v7.widget.CardView,出现错误膨胀类<unknown>。

4

在我的 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类,因此我搜索了相关问题,问题在于未添加依赖项,但我已经添加了。

我的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;
    }

有什么线索导致这种情况发生吗?

我认为这应该与内存使用有关... https://dev59.com/Feo6XIcBkEYKwwoYTS1D#23202156 和 https://dev59.com/2-o6XIcBkEYKwwoYKRHd - Lino
@Lino 我之前查看了那个问题,但是我在这里没有分配任何内存。 - remy boys
请问您能否在应用程序出现错误时附上您的 build.gradle(Module:app)文件? - Amit Upadhyay
整个Gradle文件?@AmitUpadhyay,因为我已经添加了依赖部分。 - remy boys
2个回答

2

CardView是在Android 5.0版本中添加的。当您尝试使用RecyclerView(或任何其他已添加到API 20(v5.0)或更高版本的视图)时,将出现相同的问题。虽然它们被添加到支持库中。

现在一旦您思考为什么它们被添加到支持库中?

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

我认为这可能是原因:)


你能不能讲得再清楚一点?你的意思是 RecyclerView 和 CardView 不支持 SDK 版本低于 20 吗?如果是的话,可以参考这个链接:https://dev59.com/xIrda4cB1Zd3GeqPLVx8 - remy boys
1
在你的依赖项中,你有com.android.support:cardview-v7:23.4.0。如果你将编译SDK版本从23更改为19,则尝试将v7支持更改为com.android.support:cardview-v7:19.4.0 - Amit Upadhyay
你尝试过在使用SDK 19编译时将依赖项com.android.support:cardview-v7:23.4.0更改为com.android.support:cardview-v7:19.4.0吗? - Amit Upadhyay

-1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接