MaterialCardView 之 Binary XML file line #2

宗政鸿志
2023-12-01

先上报错信息:

    android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class com.google.android.material.card.MaterialCardView
    Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class com.google.android.material.card.MaterialCardView
    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 cn.whm.bytes.weather.ui.place.adapter.PlaceDisplayAdater.onCreateViewHolder(PlaceDisplayAdater.kt:24)
        at cn.whm.bytes.weather.ui.place.adapter.PlaceDisplayAdater.onCreateViewHolder(PlaceDisplayAdater.kt:15)
        at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7078)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6235)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
        at android.view.View.layout(View.java:20948)
        at android.view.ViewGroup.layout(ViewGroup.java:6279)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
        at android.view.View.layout(View.java:20948)
        at android.view.ViewGroup.layout(ViewGroup.java:6279)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:20948)
        at android.view.ViewGroup.layout(ViewGroup.java:6279)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:20948)
        at android.view.ViewGroup.layout(ViewGroup.java:6279)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1842)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1686)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1595)
        at android.view.View.layout(View.java:20948)
        at android.view.ViewGroup.layout(ViewGroup.java:6279)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:20948)
        at android.view.ViewGroup.layout(ViewGroup.java:6279)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1842)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1686)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1595)
        at android.view.View.layout(View.java:20948)
        at android.view.ViewGroup.layout(ViewGroup.java:6279)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:902)
        at android.view.View.layout(View.java:20948)

一般看到这熟悉的bug,我们基本上断定,引入有问题,一般都是引入的路径不对,结果我点击了一下,可以直接点进去,说明路径

com.google.android.material.card.MaterialCardView 是没问题的,那咋办,看看版本号:
    implementation 'com.google.android.material:material:1.1.0'

降版本试试

  implementation 'com.google.android.material:material:1.0.0'

成功,还能说什么呢,Google测试工作也有漏洞,哈哈。

 类似资料: