Android 自定义视图错误:二进制XML文件行#0:必须提供布局#宽度属性
我在我的应用程序中实现了一个简单的自定义视图,但在尝试使用它时,我总是会遇到以下运行时错误:Android 自定义视图错误:二进制XML文件行#0:必须提供布局#宽度属性,android,android-layout,android-view,android-custom-view,android-xml,Android,Android Layout,Android View,Android Custom View,Android Xml,我在我的应用程序中实现了一个简单的自定义视图,但在尝试使用它时,我总是会遇到以下运行时错误: java.lang.RuntimeException: Unable to start activity ... Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class EmptyReviewView Caused by: andro
java.lang.RuntimeException: Unable to start activity ...
Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class EmptyReviewView
Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class EmptyReviewView
Caused by: java.lang.reflect.InvocationTargetException
Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: You must supply a layout_width attribute.
原因:java.lang.UnsupportedOperationException:二进制XML文件行#0:必须提供布局宽度属性
我查找了任何缺少的布局宽度,但没有找到任何。这是我的自定义视图的XML:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:parentTag="android.widget.LinearLayout">
<ImageView
android:layout_width="68dp"
android:layout_height="68dp"
android:layout_marginBottom="6dp"
android:layout_marginTop="24dp"
android:scaleType="fitCenter"
app:srcCompat="@drawable/ic_review_hand_stars" />
<TextView
style="@style/TextGuide.Body.1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="4dp"
android:layout_marginTop="6dp"
android:gravity="center"
android:text="@string/no_reviews_yet"
android:textAppearance="@style/TextGuide.Body.1"
android:textColor="@color/fib_color_grey_6" />
<TextView
style="@style/TextGuide.Body.1"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="24dp"
android:layout_marginTop="4dp"
android:gravity="center"
android:text="@string/be_the_first_one_to_review_this_item"
android:textAppearance="@style/TextGuide.Body.1"
android:textColor="@color/fib_color_grey_5" />
<TextView />
</merge>
这就是我在片段/活动布局上使用它的方式:
<EmptyReviewView
android:id="@+id/no_ratings_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
这个东西甚至可以在布局预览中正确显示,但一旦显示片段,应用程序就会崩溃。从片段中注释出视图使其工作,因此这无疑是一个问题:
在您的布局中,在最后一个
文本视图之后,您有这个
。删除它,然后应用程序将正常运行。这是一个较老的问题,但为了他人的利益共享答案
您可能希望检查维度文件或“仅指定了数字但没有所需后缀的值”,如'sp'或'dp'
在我们的例子中,这种难以发现的运行时错误(“您必须提供布局宽度属性”)是由于维度文件中的值没有必要的后缀,如'sp'或'dp'(仅数字作为值给出,但没有类似于'dp'的后缀)
示例:16
而不是16sp
,其中“16sp”只是“16”,导致系统抛出运行时错误。不幸的是,这种类型的错误在编码过程中从未出现,编译成功,但在尝试呈现相应的活动时会使应用程序崩溃。希望这能帮助一些人面对这个问题
注:
- 维度文件不接受没有所需后缀的数值
- 感谢帮助我们的答案
是否将合并标记作为父布局?是否有其他版面正在使用此版面文件?您是一个救生员!花了一整天的时间在这上面,却没能让它工作。真不敢相信这是一件如此愚蠢的事情!再次感谢!
<EmptyReviewView
android:id="@+id/no_ratings_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />