【问题标题】:SDK 19 and below is not recognising android.support.v7.widget.CardView , Error inflating class <unknown>SDK 19 及以下版本无法识别 android.support.v7.widget.CardView ,错误膨胀类 <unknown>
【发布时间】:2016-07-02 06:52:39
【问题描述】:

在我的 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 类,所以我搜索了相关问题,问题不是添加依赖项,但我已经添加了它

我的摇篮:

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

任何线索为什么会发生这种情况??

【问题讨论】:

标签: android android-cardview layout-inflater backwards-compatibility


【解决方案1】:

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

现在,一旦您想到为什么将它们添加到支持库中?

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

我想这可能是我的原因:)

【讨论】:

  • 你能说得清楚一点吗?你的意思是 RecyclerView , CardView 与 SDK stackoverflow.com/questions/29982535/…
  • 在您的依赖项中有com.android.support:cardview-v7:23.4.0,如果您将编译SDK 版本从23 更改为19,请尝试将v7 支持更改为com.android.support:cardview-v7:19.4.0
  • 您是否尝试在使用 SDK 19 编译时将依赖项 com.android.support:cardview-v7:23.4.0 更改为 com.android.support:cardview-v7:19.4.0??
【解决方案2】:

转到项目结构并单击添加库依赖项并选择 com.android.support:cardview-v7:23.2.1 按 ok 它将解决您的问题。点击下面的链接了解一下

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

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

【讨论】:

  • 然后下一步该怎么做?
  • 试试这个,我已经修改了。
  • 现在的错误是什么?我也添加了图片,请检查它们。
猜你喜欢
  • 1970-01-01
  • 2015-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多