膨胀android.support.v7.widget.CardView会引发错误

时间:2019-04-05 16:15:12

标签: java android android-cardview layout-inflater

我在尝试运行应用程序时遇到以下错误

android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.support.v7.widget.CardView

这是我的Logcat:

FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.support.v7.widget.CardView
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.CardView
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
    at android.view.LayoutInflater.createView(LayoutInflater.java:658)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:801)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at com.example.luke.diary.EntryListAdapter.onCreateViewHolder(EntryListAdapter.java:24)
    at com.example.luke.diary.EntryListAdapter.onCreateViewHolder(EntryListAdapter.java:14)
    at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6794)
    at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5975)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
    at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
    at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
    at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
    at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
    at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3336)
    at android.view.View.measure(View.java:23470)
    at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1210)
    at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1550)
    at android.view.View.measure(View.java:23470)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6831)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
    at android.view.View.measure(View.java:23470)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6831)
    at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:401)
    at android.view.View.measure(View.java:23470)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6831)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at android.view.View.measure(View.java:23470)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6831)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1565)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:726)
    at android.view.View.measure(View.java:23470)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6831)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at com.android.internal.policy.DecorView.onMeasure(DecorView.java:823)
    at android.view.View.measure(View.java:23470)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2941)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1740)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2028)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1624)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7926)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1079)
    at android.view.Choreographer.doCallbacks(Choreographer.java:885)

这是我的Java代码

public EntryListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.entry_item, parent, false);
  EntryListViewHolder customViewHolder = new EntryListViewHolder(itemView);

  return customViewHolder;

}

和布局

<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
    app:cardCornerRadius="@dimen/corner_radius"
    app:cardElevation="@dimen/elevation"
    app:contentPadding="@dimen/padding">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/title"
        .../>

    <TextView
        android:id="@+id/content"
        .../>

    <TextView
        android:id="@+id/date"
        .../>
</android.support.constraint.ConstraintLayout>

我已经尝试过将cardview删除并将其读入app / build.gradle,但这对我不起作用

1 个答案:

答案 0 :(得分:0)

在.xml中替换为

android.support.v7.widget.CardView
android.support.constraint.ConstraintLayout

带有那个

androidx.cardview.widget.CardView
androidx.constraintlayout.widget.ConstraintLayout

并在app.gradle中添加依赖项

implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
implementation 'androidx.cardview:cardview:1.0.0'