Sayan98 Sayan98 - 5 months ago 23
Android Question

Error:You must supply a layout_width attribute error. Although I've supplied it

Here's the layout which is causing problems.

<?xml version="1.0" encoding="utf-8"?>

<!-- Drawer Layout -->

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
android:fitsSystemWindows="true">

<!--Main content-->

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/mtoolbar"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:layout_width="match_parent"
app:theme="@style/ThemeOverlay.AppCompat.Dark"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:layout_height="wrap_content">

</android.support.v7.widget.Toolbar>


<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/mtoolbar"
android:layout_centerInParent="true"/>

</RelativeLayout>

<!--Navigation Drawer-->
<android.support.design.widget.NavigationView
android:id="@+id/main_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/menu_drawer"/>
</android.support.v4.widget.DrawerLayout>


Here is the
drawer_header.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="@drawable/header"
android:padding="16dp"
android:orientation="vertical"
android:gravity="bottom">

<TextView
android:textColor="#ffffff"
android:textSize="18sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World "
/>
</LinearLayout>


And here's the logcat output

06-15 21:38:05.125 14798 14798 I art Late-enabling -Xcheck:jni
06-15 21:38:05.323 14798 14798 W System ClassLoader referenced unknown path: /data/app/xyz.sayangoswami.opoforums-2/lib/arm
06-15 21:38:05.472 14798 14798 I AppCompatViewInflater app:theme is now deprecated. Please move to using android:theme instead.
06-15 21:38:05.515 14798 14798 D AndroidRuntime Shutting down VM
06-15 21:38:05.520 14798 14798 E AndroidRuntime FATAL EXCEPTION: main
06-15 21:38:05.520 14798 14798 E AndroidRuntime Process: xyz.sayangoswami.opoforums, PID: 14798
06-15 21:38:05.520 14798 14798 E AndroidRuntime java.lang.RuntimeException: Unable to start activity ComponentInfo{xyz.sayangoswami.opoforums/xyz.sayangoswami.opoforums.MainActivity}: android.view.InflateException: Binary XML file line #39: Binary XML file line #39: Error inflating class android.support.design.widget.NavigationView
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.app.ActivityThread.-wrap11(ActivityThread.java)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.os.Handler.dispatchMessage(Handler.java:102)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.os.Looper.loop(Looper.java:148)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.app.ActivityThread.main(ActivityThread.java:5417)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at java.lang.reflect.Method.invoke(Native Method)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-15 21:38:05.520 14798 14798 E AndroidRuntime Caused by: android.view.InflateException: Binary XML file line #39: Binary XML file line #39: Error inflating class android.support.design.widget.NavigationView
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at xyz.sayangoswami.opoforums.MainActivity.onCreate(MainActivity.java:28)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.app.Activity.performCreate(Activity.java:6251)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
06-15 21:38:05.520 14798 14798 E AndroidRuntime ... 9 more
06-15 21:38:05.520 14798 14798 E AndroidRuntime Caused by: android.view.InflateException: Binary XML file line #39: Error inflating class android.support.design.widget.NavigationView
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.view.LayoutInflater.createView(LayoutInflater.java:645)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
06-15 21:38:05.520 14798 14798 E AndroidRuntime ... 17 more
06-15 21:38:05.520 14798 14798 E AndroidRuntime Caused by: java.lang.reflect.InvocationTargetException
06-15 21:38:05.520 14798 14798 E AndroidRuntime at java.lang.reflect.Constructor.newInstance(Native Method)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.view.LayoutInflater.createView(LayoutInflater.java:619)
06-15 21:38:05.520 14798 14798 E AndroidRuntime ... 22 more
06-15 21:38:05.520 14798 14798 E AndroidRuntime Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: You must supply a layout_width attribute.
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.support.design.internal.NavigationMenuPresenter.inflateHeaderView(NavigationMenuPresenter.java:189)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.support.design.widget.NavigationView.inflateHeaderView(NavigationView.java:263)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.support.design.widget.NavigationView.<init>(NavigationView.java:174)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.support.design.widget.NavigationView.<init>(NavigationView.java:96)
06-15 21:38:05.520 14798 14798 E AndroidRuntime ... 24 more
06-15 21:38:05.520 14798 14798 E AndroidRuntime Caused by: java.lang.UnsupportedOperationException: Binary XML file line #2: You must supply a layout_width attribute.
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:709)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:6889)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:7071)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.widget.LinearLayout$LayoutParams.<init>(LinearLayout.java:1911)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1816)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1815)
06-15 21:38:05.520 14798 14798 E AndroidRuntime at android.view.LayoutInflater.inflate(LayoutInflater.java:502)
06-15 21:38:05.520 14798 14798 E AndroidRuntime ... 29 more


Although I've supplied the layout_width parameter for the
DrawerLayout
it is still showing an error.

If it helps, I'm using AppCompat v7 and Design support libraries with compileSdkVersion 21 and buildToolsVersion 21.1.0

Thanks in advance.

The problem is resolved.



The problem was that I had
http://schemas.android.com/apk/res/android
in capitals that is
HTTP://SCHEMAS.ANDROID.COM/APK/RES/ANDROID
in
drawer_header.xml
line 2 .

Answer

You should be seeing errors in your drawer_header.xml file, the namespace should not be all caps:

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

Not:

xmlns:android="HTTP://SCHEMAS.ANDROID.COM/APK/RES/ANDROID"