Warm tip: This article is reproduced from serverfault.com, please click

android-自定义视图错误:二进制XML文件第0行:你必须提供layout_width属性

(android - Custom View Error: Binary XML file line #0: You must supply a layout_width attribute)

发布于 2018-03-05 20:55:31

我已经在我的应用程序中实现了一个简单的自定义视图,但是在尝试使用它时,总是会遇到此运行时错误:

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);
  }

}

这就是我在“片段/活动”布局上使用它的方式:

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

该内容甚至可以正确显示在“布局预览”中,但是一旦显示该片段,应用程序便会崩溃。从片段中注释掉视图即可使其正常工作,因此这绝对是问题所在:

布局预览的屏幕截图

Questioner
Felipe Ribeiro R. Magalhaes
Viewed
0
JaFer 2018-03-06 05:22:10

在你的布局中,在最后一个之后TextView有this <TextView />删除它,然后该应用程序将正确运行。