DrawerLayout inside ConstaintLayout - java.lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY

本小妞迷上赌 提交于 2019-12-22 16:52:28

问题


Here my xml layout file:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.support.v4.widget.DrawerLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent">    
    </android.support.v4.widget.DrawerLayout>        

</android.support.constraint.ConstraintLayout>

But when I start my Android app I get error (on Runtime):

E/AndroidRuntime: FATAL EXCEPTION: main
 java.lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY.
     at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:994)
     at android.view.View.measure(View.java:15848)
     at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934)

回答1:


Your problem is

java.lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY.

So you must set height and width MeasureSpec.EXACTLY .

If you use this in your code .The height and width will be MeasureSpec.EXACTLY .

<android.support.v4.widget.DrawerLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">

Try this in your code .

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout     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.support.v4.widget.DrawerLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">    
</android.support.v4.widget.DrawerLayout>

</android.support.constraint.ConstraintLayout>



回答2:


Drawer layout must be the root element in layout. That's why error occurs.

Also, it should have two children - first is a nav view and a second is a layout which includes your content



来源:https://stackoverflow.com/questions/47001482/drawerlayout-inside-constaintlayout-java-lang-illegalargumentexception-drawer

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