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

I have implemented a simple custom view in my app, but I am always getting this run time error when trying to use it:

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.

Caused by: java.lang.UnsupportedOperationException: Binary XML file line #0: You must supply a layout_width attribute.

I looked for any missing layout_width but did not find any. This is my custom view's 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>

And this is the custom view's class:

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

}

And this is how I use it on Fragments/Activities layouts:

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

The thing even displays correctly in the Layout Preview, but the app crashes as soon as the fragment is displayed. Commenting out the view from the fragment makes it work, so this is definitely the issue:

Screenshot of the layout preview

Upvotes: 1

Views: 2263

Answers (2)

Simmam
Simmam

Reputation: 135

This is an older question but sharing the answer for the benefit of others.

One may want to check the dimension file or "value assigned with just numbers but without required suffix" like 'sp' or 'dp'.

This hard to find runtime error ("You must supply a layout_width attribute") in our case was due to value in dimension file without required suffix like 'sp' or 'dp' (only number given as value but without suffix similar to 'dp').

Example: <dimen name="Setting_txt_size">16</dimen> instead of <dimen name="Setting_txt_size">16sp</dimen> where '16sp' was just '16' causing the system to throw runtime error. This type of error unfortunately never shows up during coding, compiles successfully but crashes the app when the respective activity is tried for rendering. Hope this helps someone facing this issue.

Note:

  • dimension file does not accepts numeric values without required suffix
  • Thanks to the answer that helped us available here

Upvotes: 3

JaFer
JaFer

Reputation: 213

In your layout, after the last TextView you have this <TextView />. Remove it and then the app will run correctly.

Upvotes: 2

Related Questions