Android activity Binary XML file line : You must supply a layout_width attribute

我怕爱的太早我们不能终老 提交于 2021-02-06 14:55:09

问题


I've read about it already, but still it's here.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".TaskEditActivity" >

So, the exception java.lang.RuntimeException: Binary XML file line #34: You must supply a layout_width attribute.

Attribute is there, schema too... Solution?


回答1:


Also i recommend check your dimens. You may have layout width in dimens, you go to it and you have value like "150" not "150dp" (missing dp) also cause this (that was my case)




回答2:


I had a dimension for a given width but it wasn't present in the default dimens.xml

res/
   values-w320dp/
          dimens.xml -> ring_radius=5dp
   values/
          dimens.xml -> missing ring_radius



回答3:


It's clear. Child elements missed layout_width attribute.




回答4:


Your problem is not in this layout element, its on the inner one, take a look at line number 34. There is is another user interface element that lacks the layout_width attribute.




回答5:


For me it was having an extra attribute on one of my view groups (scrollview):

xmlns:android="http://schemas.android.com/apk/res/android"

After that there was I forgot to change my casting of EditText to TextView:

Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText




回答6:


This can happen if you apply or don't apply the correct theme on a fragment/activity.

For example Leanback's OnboardingSupportFragment requires Theme.Leanback.Onboarding, which you have to apply either on the activity in the manifest android:theme="@style/Theme.Leanback.Onboarding" or in the fragment overriding onProvideTheme()

@Override
public int onProvideTheme() {
    return R.style.Theme_Leanback_Onboarding;
}



回答7:


For me, inside one of my layout.xml files, I had

<ImageView
    android:id="@+id/row_1_col_0"
    android:layout_width="@string/default_picture_size"
    android:layout_height="@string/default_picture_size"
    android:layout_weight="1"
    android:background="@drawable/tile"
    android:onClick="mClickMethod" >
</ImageView>

and inside strings.xml, I had wrap_content

and so it was showing this in Android Studio:

<ImageView
    android:id="@+id/row_1_col_0"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@drawable/tile"
    android:onClick="mClickMethod" >
</ImageView>

I thought everything would work because there were no errors and the application compiled and ran. However, there was a run time error that said I didn't set the layout_width.

Once I changed layout_width and layout_height from:

android:layout_width="@string/default_picture_size"
android:layout_height="@string/default_picture_size"

to

android:layout_width="wrap_content"
android:layout_height="wrap_content"

everything worked.




回答8:


just incase all the solutions don't work
clean project -> run cheers



来源:https://stackoverflow.com/questions/16999023/android-activity-binary-xml-file-line-you-must-supply-a-layout-width-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!