Caused by: android.view.InflateException: Binary XML file line

1、异常信息

Process: com.example.devbytes, PID: 32509
     android.view.InflateException: Binary XML file line #23 in com.example.devbytes:layout/devbyte_item: Binary XML file line #23 in com.example.devbytes:layout/devbyte_item: Error inflating class com.google.android.material.card.MaterialCardView
     Caused by: android.view.InflateException: Binary XML file line #23 in com.example.devbytes:layout/devbyte_item: Error inflating class com.google.android.material.card.MaterialCardView

2、分析流程

根据搜索以及分析,可以知道是因为 inflate layout file时出错,那么可能的原因是:

  1. 自定义View类定义出错,常见的为少了构建函数:public MyView(Context context,AttributeSet paramAttributeSet)。该错误是使用com.google.android.material.card.MaterialCardView时出错,所以不符合;
  2. 定义类引用为题:如写一个自定义view,引用的时候需要用完整路径;
  3. 资源文件不存在或者找不到。不存在比较好判别,存在但是找不到可能的原因是不同分辨率的drawable下文件夹的资源,如果只在一个文件下放了资源文件,不同的分辨率设备的会报错;
  4. 可能是编码问题;
  5. 内存泄漏;
  6. Application主题与组件风格不符合,比如application主题是Theme.AppCompat,但是组件却是com.google.android.material,这时,也可能出错(本文就是如此);
  7. 风格使用对象不对,比如TextViewText Style时,应该使用style,而不是android:style

3、所获

  1. 遇到layout file无法inflate时,应该尽可能地分析是什么原因导致无法使用,可能的原因就是某个地方出错,导致无法使用,可以考虑的方向:自定义View是否正确;属性是否设置正确;是否引用了不存在的资源;使用View方法是否正确;主题是否使用一致等。
  2. 仔细分析异常栈,由上向下,越往下,原因可能就更明显。

4、完整异常信息

  Process: com.example.devbytes, PID: 32509
  android.view.InflateException: Binary XML file line #23 in com.example.devbytes:layout/devbyte_item: Binary XML file line #23 in com.example.devbytes:layout/devbyte_item: Error inflating class com.google.android.material.card.MaterialCardView
  Caused by: android.view.InflateException: Binary XML file line #23 in com.example.devbytes:layout/devbyte_item: 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:863)
  	at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1019)
  	at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:970)
  	at android.view.LayoutInflater.inflate(LayoutInflater.java:669)
  	at android.view.LayoutInflater.inflate(LayoutInflater.java:542)
  	at androidx.databinding.DataBindingUtil.inflate(DataBindingUtil.java:126)
  	at androidx.databinding.DataBindingUtil.inflate(DataBindingUtil.java:95)
  	at com.example.devbytes.ui.DevByteAdapter.onCreateViewHolder(DevByteFragment.kt:176)
  	at com.example.devbytes.ui.DevByteAdapter.onCreateViewHolder(DevByteFragment.kt:156)
  	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:23488)
  	at android.view.ViewGroup.layout(ViewGroup.java:6575)
  	at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
  	at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
  	at android.view.View.layout(View.java:23488)
  	at android.view.ViewGroup.layout(ViewGroup.java:6575)
  	at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
  	at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
  	at android.view.View.layout(View.java:23488)
  	at android.view.ViewGroup.layout(ViewGroup.java:6575)
  	at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
  	at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
  	at android.view.View.layout(View.java:23488)
  	at android.view.ViewGroup.layout(ViewGroup.java:6575)
  	at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
  	at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
  	at android.view.View.layout(View.java:23488)
  	at android.view.ViewGroup.layout(ViewGroup.java:6575)
  	at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:536)
  	at android.view.View.layout(View.java:23488)
  	at android.view.ViewGroup.layout(ViewGroup.java:6575)
  	at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
  	at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
  	at android.view.View.layout(View.java:23488)
  	at android.view.ViewGroup.layout(ViewGroup.java:6575)
  	at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1841)
  	at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
2023-09-06 21:54:16.666 32509-32509 AndroidRuntime          com.example.devbytes                 E  	at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
  	at android.view.View.layout(View.java:23488)
  	at android.view.ViewGroup.layout(ViewGroup.java:6575)
  	at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
  	at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
  	at com.android.internal.policy.DecorView.onLayout(DecorView.java:867)
  	at android.view.View.layout(View.java:23488)
  	at android.view.ViewGroup.layout(ViewGroup.java:6575)
  	at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:4119)
  	at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3495)
  	at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2416)
  	at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:9478)
  	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1242)
  	at android.view.Choreographer.doCallbacks(Choreographer.java:996)
  	at android.view.ChoreographerExtImpl.checkScrollOptSceneEnable(ChoreographerExtImpl.java:383)
  	at android.view.Choreographer.doFrame(Choreographer.java:865)
  	at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1227)
  	at android.os.Handler.handleCallback(Handler.java:938)
  	at android.os.Handler.dispatchMessage(Handler.java:99)
  	at android.os.Looper.loopOnce(Looper.java:233)
  	at android.os.Looper.loop(Looper.java:344)
  	at android.app.ActivityThread.main(ActivityThread.java:8249)
  	at java.lang.reflect.Method.invoke(Native Method)
  	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:589)
  	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1071)
  Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
  	at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:247)
  	at com.google.android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.java:216)
  	at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:144)
  	at com.google.android.material.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.java:76)
  	at com.google.android.material.card.MaterialCardView.(MaterialCardView.java:170)
  	at com.google.android.material.card.MaterialCardView.(MaterialCardView.java:160)

你可能感兴趣的:(bug,合集,android,xml)