例外:android.view.InflateException:二进制XML文件行#2:错误膨胀类android.support.v7.widget.CardView

时间:2014-10-31 13:19:59

标签: android android-support-library android-cardview

我在我的Android应用中使用CardView支持库。我尝试了4-5个小时来解决这个问题,但我无法取得成功。

我在项目中添加了CardView作为库。 v7 appcompat也

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:orientation="horizontal"
    app:cardCornerRadius="4dp" >

但我的应用程序崩溃了以下日志

10-31 18:41:39.577: E/AndroidRuntime(5666): FATAL EXCEPTION: main
10-31 18:41:39.577: E/AndroidRuntime(5666): Process: com.xyzladdoo, PID: 5666
10-31 18:41:39.577: E/AndroidRuntime(5666): android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.CardView
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at com.xyzladdoo.adapters.RecycleViewAdapter.onCreateViewHolder(RecycleViewAdapter.java:85)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at com.xyzladdoo.adapters.RecycleViewAdapter.onCreateViewHolder(RecycleViewAdapter.java:1)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:2915)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:2511)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.support.v7.widget.LinearLayoutManager$RenderState.next(LinearLayoutManager.java:1425)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:999)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:524)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1461)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:1600)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.View.layout(View.java:14860)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.ViewGroup.layout(ViewGroup.java:4643)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:543)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.View.layout(View.java:14860)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.ViewGroup.layout(ViewGroup.java:4643)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.View.layout(View.java:14860)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.ViewGroup.layout(ViewGroup.java:4643)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.View.layout(View.java:14860)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.ViewGroup.layout(ViewGroup.java:4643)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1594)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.View.layout(View.java:14860)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.ViewGroup.layout(ViewGroup.java:4643)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:887)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.View.layout(View.java:14860)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.ViewGroup.layout(ViewGroup.java:4643)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.View.layout(View.java:14860)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.ViewGroup.layout(ViewGroup.java:4643)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.View.layout(View.java:14860)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.ViewGroup.layout(ViewGroup.java:4643)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.View.layout(View.java:14860)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.ViewGroup.layout(ViewGroup.java:4643)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.View.layout(View.java:14860)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.ViewGroup.layout(ViewGroup.java:4643)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.View.layout(View.java:14860)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.ViewGroup.layout(ViewGroup.java:4643)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2013)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1770)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at android.view.Choreographer.doFrame(Choreographer.java:544)
10-31 18:41:39.577: E/AndroidRuntime(5666):     at an

有任何帮助吗?提前谢谢。

7 个答案:

答案 0 :(得分:7)

让cardview作为库导入:
转到文件 - &gt;导入 - &gt;现有的Android代码进入工作区 - &gt;浏览(转到sdk / extras / android / support / v7 / cardview) - &gt;点击确定 - &gt;单击“完成”

您的项目浏览器现在将cardview显示为项目。

右键单击cardview项目 - &gt;属性 - &gt; Android(左窗格) - &gt;启用isLibrary(勾选复选框) - &gt;申请 - &gt;确定

现在右键单击您的项目 - &gt;属性 - &gt; Android(左窗格) - &gt;添加(在库下) - &gt; cardview - &gt;申请 - &gt;确定

现在再次右键点击您的项目 - &gt;构建路径 - &gt;配置构建路径 - &gt;在“项目”选项卡下,添加cardview

你完成了。
Source

答案 1 :(得分:2)

(对于intellij用户)

  • 点击您的项目,
  • 右键单击 - &gt;打开模块设置
  • 从\ sdk \ extras \ android \ support \ v7 \
  • 导入cardview
  • 将.jar文件添加到cardview模块
  • 点击您的项目并将模块依赖性提供给cardview
  • 现在,点击cardview上的(+)按钮 - &gt; android - &gt;在顶部,您将看到一个复选框(库模块),启用它。
  • 单击“确定”并关闭设置对话框。
  • 重建项目并运行它。

答案 2 :(得分:1)

支持包含编译类以外的资源(如XML)的库不能仅作为jar添加。

请按照以下步骤进行操作:

https://developer.android.com/tools/support-library/setup.html#libs-with-res

如果可行的话,现在就告诉我们。

答案 3 :(得分:1)

解决此问题。 首先,您必须从\ sdk \ extras \ android \ support

添加cardview
  1. 关闭主项目。
  2. 删除android-support-v7-appcompat。
  3. 重启Eclipse。
  4. 添加android-support-v7-appcompat。
  5. 清洁,构建项目。
  6. 然后打开主项目并构建所有项目。
  7. 错误仍然存​​在。重启eclipse。那就是它。
  8. 这适合我。

答案 4 :(得分:0)

试试这个..

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="200dp"
    android:layout_height="200dp"
    card_view:cardCornerRadius="4dp">


</android.support.v7.widget.CardView>

参考Create Cards

答案 5 :(得分:0)

尝试使用外部线性布局。

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    ... >
    <!-- A CardView that contains a TextView -->
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="200dp"
        android:layout_height="200dp"
        card_view:cardCornerRadius="4dp">

        <TextView
            android:id="@+id/info_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v7.widget.CardView>
</LinearLayout>

答案 6 :(得分:0)

我和你有同样的问题。

Eclipse 中,当您从目录\ sdk \ extras \ android \ support \ v7 \ cardview导入cardview时,请按照官方文档中的“使用资源添加库 - &gt; Eclipse”中的注释步骤操作: https://developer.android.com/intl/es/tools/support-library/setup.html#libs-with-res

在将cardview作为库添加到项目之前,请转到 cardview项目的“属性 - &gt; Android”和添加android-support-v7-appcompat库。不要忘记检查“IsLibrary”。

现在,您可以将cardview作为库添加到项目中。清理两个项目以刷新更改。

我希望它可以帮助你。