当使用4.7 WXGA API 23时,我得到了 "android.view.InflateException.Binary XML file line #7: Error inflating class <unknown> "的错误。Binary XML file line #7: Binary XML file line #7: Error inflating class" 的错误。
但在更高的版本中,它都能正常工作,我已经为我的问题添加了代码和xml布局,不明白为什么会发生这样的情况。
发生此错误的代码
@NonNull
@Override
public PostsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_list_layout,
parent, false);
return new PostsHolder(v);
}
XML Layout for recyclerview Item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/shadow_300_50"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:padding="5dp"
android:orientation="horizontal"
>
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/profile"
android:id="@+id/post_user_profile_image"
android:scaleType="centerCrop"
android:layout_marginLeft="4dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Username"
android:id="@+id/post_item_username"
android:layout_marginLeft="5dp"
android:layout_marginTop="8dp"
android:textAlignment="textStart"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="@android:color/black"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Time"
android:id="@+id/post_item_time"
android:layout_marginLeft="257dp"
android:layout_marginRight="5dp"
android:layout_marginTop="8dp"
android:textAlignment="textStart"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="@android:color/black"/>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:id="@+id/post_item_title"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:textAlignment="textStart"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="@android:color/black"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="5dp"
android:padding="2dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="post_item_Category"
android:textColor="@color/colorPrimaryDark"
android:textSize="13sp"
android:textStyle="bold"
android:id="@+id/post_item_Category"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Post_item_SpinnerC"
android:textColor="@color/colorPrimaryDark"
android:textSize="13sp"
android:id="@+id/post_item_SpinnerC"
android:layout_marginLeft="7dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/post_item_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginRight="3dp"
android:padding="3dp"
android:text="Post Description"
android:textColor="@android:color/black"
android:textSize="14dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:padding="2dp">
<Button
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mark"
app:srcCompat="@drawable/ic_heart_normal"
android:background="@drawable/button"/>
<Button
android:id="@+id/commenting_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="140dp"
android:text="Comment"
android:background="@drawable/button"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
问题是 不存在一个用于创建小组件的类的构造函数 或无法找到。 LayoutInflater类(在sdk中,即:Android/Sdk/sources/android-17/android/view/LayoutInflater.java)使用对widget名称的反射(代码自省)来构建widget对象。 在你的情况下,我猜测是'de.hdodenhof.circleimageview.CircleImageView',在我的情况下是'androidx.appcompat.widget.Toolbar'。
找到问题埋在好几层异常中,只有那个(相当无用的 'Binary XML file line #7: Error inflating class...'(二进制XML文件第7行:膨胀类错误)。 消息出现。 我设置了Android Studio,在 android.view.InflateException 异常。
底部的'ie'InflateException包含一个... java.lang.reflect.InvocationTargetException异常。导致这种情况的原因是 android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2d=0x7f03008f a=-1}异常(在我的例子中)
在LayoutInflator代码中。
林616是他们在InflationException中嵌入异常 "e "的地方。 利用AS的'评价表达式'功能,我调用了 'e.getStackTrace()'。 其中显示了错误真正发生的位置。
StackTrace第2行显示是LayoutInflater第587行(在上面的代码图中看到红色背景)引发了异常。 往上看stackTrace第0行,是java.lang.reflect.Constructor.constructNative方法抛出了原始异常。
LayoutInflator第587行是想实例化androidx.appcompat.widget.Toolbar(对我来说),正在寻找一个有2个参数的Toolbar构造函数。
1) Context: ref to activity in which the layout was being inflated
2) AttributeSet: the opaque XmlBlock object named 'attrs'
我通过在'this.mConstructorArgs'上使用Expression Evaluator发现了这一点(见代码第584行)。
所以,对我来说,它找不到一个带有Toolbar(Activity, XmlBlock)签名的构造函数。 所以,我必须研究一下Toolbar被配置定义在哪里,并找出它有哪些构造函数。
通常人们给出的 "解决方案 "是 如本题,就是清除缓存,重置支持库,用最新的依赖关系运行等等,这些都不能让你找到根本原因,有时还不起作用。
我在这里把这个记录下来,有点是为了我自己以后的问题--也是恳求Android开发者给出更清晰的错误信息。 'Binary XML file line #7: Error inflating'并没有什么帮助,一些Binary XML文件的第7行并没有给出和线索。
@ramneek,'de.hdodenhof.circleimageview.CircleImageView'是你充气的唯一一个不寻常的widget,一定是有一些方法或者构造函数的那个类widget,而充气器找不到。