自定义视图错误:二进制XML文件行#0:您必须提供layout_width属性

时间:2018-03-05 20:55:32

标签: 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: 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:您必须提供layout_width属性。

我查找了任何缺少的layout_width但没找到任何。这是我的自定义视图的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>

这是自定义视图的课程:

public class EmptyReviewView extends LinearLayout {

  public EmptyReviewView(Context context) {
      this(context, null, 0);
  }

  public EmptyReviewView(Context context, @Nullable AttributeSet attrs) {
      this(context, attrs, 0);
  }

  public EmptyReviewView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
      super(context, attrs, defStyleAttr);
      initView();
  }

  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public EmptyReviewView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
      super(context, attrs, defStyleAttr, defStyleRes);
      initView();
  }

  private void initView() {
      LayoutInflater.from(getContext()).inflate(R.layout.view_review_empty, this, true);
      setOrientation(VERTICAL);
      setGravity(Gravity.CENTER);
  }

}

这就是我在Fragments / Activities布局中使用它的方式:

<EmptyReviewView
            android:id="@+id/no_ratings_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

甚至可以在布局预览中正确显示内容,但是一旦显示片段,应用就会崩溃。从片段中注释掉视图会使它工作,所以这绝对是个问题:

Screenshot of the layout preview

2 个答案:

答案 0 :(得分:1)

在您的布局中,在最后TextView之后,您拥有此<TextView />。删除它然后应用程序将正确运行。

答案 1 :(得分:0)

这是一个较旧的问题,但为了他人的利益分享答案。

人们可能想检查维度文件或“仅分配数字但没有必需后缀的值”,如'sp''dp'强>.

在我们的案例中,这个难以发现的运行时错误(“您必须提供 layout_width 属性”)是由于维度文件中的值没有必要的后缀,如 'sp' 或 'dp'(仅给出数字作为值,但没有类似于 'dp' 的后缀)。

示例:<dimen name="Setting_txt_size">16</dimen> 而不是 <dimen name="Setting_txt_size">16sp</dimen>,其中 '16sp' 只是 '16' 导致系统抛出运行时错误。不幸的是,这种类型的错误在编码过程中从未出现过,编译成功但在尝试渲染相应的 Activity 时会导致应用程序崩溃。希望这对遇到此问题的人有所帮助。

注意:

  • 维度文件不接受没有必需后缀的数值
  • 感谢帮助我们的回答available here