我在我的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
有任何帮助吗?提前谢谢。
答案 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用户)
答案 2 :(得分:1)
支持包含编译类以外的资源(如XML)的库不能仅作为jar添加。
请按照以下步骤进行操作:
https://developer.android.com/tools/support-library/setup.html#libs-with-res
如果可行的话,现在就告诉我们。
答案 3 :(得分:1)
解决此问题。 首先,您必须从\ sdk \ extras \ android \ support
添加cardview这适合我。
答案 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作为库添加到项目中。清理两个项目以刷新更改。
我希望它可以帮助你。