添加TextView后:java.lang.IllegalArgumentException:必须使用MeasureSpec.EXACTLY测量DrawerLayout

时间:2017-01-22 15:49:23

标签: java android

我有一个带导航抽屉和框架布局的活动。我想在底部显示一个TextView。我已将FrameLayout和TextView放在LinearLayout中。一旦我运行活动,我就会收到此错误。

这是logcat:

01-22 19:43:20.862 18091-18091/com.parassidhu.cdlumaths E/UncaughtException: java.lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY.
                                                                                 at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1042)
                                                                                 at android.view.View.measure(View.java:17908)
                                                                                 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5704)
                                                                                 at android.widget.FrameLayout.onMeasure(FrameLayout.java:465)
                                                                                 at android.view.View.measure(View.java:17908)
                                                                                 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5704)
                                                                                 at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1692)
                                                                                 at android.widget.LinearLayout.measureVertical(LinearLayout.java:760)
                                                                                 at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                                                                                 at android.view.View.measure(View.java:17908)
                                                                                 at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:846)
                                                                                 at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:512)
                                                                                 at android.view.View.measure(View.java:17908)
                                                                                 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5704)
                                                                                 at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:714)
                                                                                 at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
                                                                                 at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1375)
                                                                                 at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:784)
                                                                                 at android.view.View.measure(View.java:17908)
                                                                                 at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1081)
                                                                                 at android.view.View.measure(View.java:17908)
                                                                                 at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1081)
                                                                                 at android.view.View.measure(View.java:17908)
                                                                                 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5704)
                                                                                 at android.widget.FrameLayout.onMeasure(FrameLayout.java:465)
                                                                                 at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                                                                                 at android.view.View.measure(View.java:17908)
                                                                                 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5704)
                                                                                 at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1692)
                                                                                 at android.widget.LinearLayout.measureVertical(LinearLayout.java:760)
                                                                                 at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                                                                                 at android.view.View.measure(View.java:17908)
                                                                                 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5704)
                                                                                 at android.widget.FrameLayout.onMeasure(FrameLayout.java:465)
                                                                                 at android.view.View.measure(View.java:17908)
                                                                                 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5704)
                                                                                 at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1692)
                                                                                 at android.widget.LinearLayout.measureVertical(LinearLayout.java:760)
                                                                                 at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                                                                                 at android.view.View.measure(View.java:17908)
                                                                                 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5704)
                                                                                 at android.widget.FrameLayout.onMeasure(FrameLayout.java:465)
                                                                                 at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2731)
                                                                                 at android.view.View.measure(View.java:17908)
                                                                                 at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2416)
                                                                                 at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1418)
                                                                                 at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1642)
                                                                                 at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1296)
                                                                                 at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6628)
                                                                                 at android.view.Choreographer$CallbackRecord.run(Choreographer.java:800)
                                                                                 at android.view.Choreographer.doCallbacks(Choreographer.java:603)
                                                                                 at android.view.Choreographer.doFrame(Choreographer.java:572)
                                                                                 at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:786)
                                                                                 at android.os.Handler.handleCallback(Handler.java:815)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                                 at android.os.Looper.loop(Looper.java:194)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5643)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                at java.lang.reflect.Method.invoke(Me

acivity_home.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_home"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header_home"
        app:menu="@menu/activity_home_drawer" />

</android.support.v4.widget.DrawerLayout>

app_bar_home.xml

<?xml version="1.0" encoding="utf-8"?>
<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.support.design.widget.CoordinatorLayout
        android:id="@+id/rootLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:id="@+id/appbar"
        app:elevation="12dp"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:titleTextColor="@color/white"
            android:elevation="12dp"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/Theme.AppCompat.NoActionBar"
            app:theme="@style/Jatt"
            app:layout_scrollFlags="scroll|enterAlways"/>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_home" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/offline" />

    </android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>

content_home.xml (错误来自于TextView,也许还有LinearLayout)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/content_home"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.parassidhu.cdlumaths.Home"
    android:fitsSystemWindows="true"
    tools:showIn="@layout/app_bar_home">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello hi"
            android:gravity="bottom"
            android:layout_gravity="bottom"
            android:layout_weight="0"/>
    </LinearLayout>

</RelativeLayout>

3 个答案:

答案 0 :(得分:0)

您需要确保DrawerLayout身高为match_parent

app_bar_home布局中删除抽屉布局标记:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:id="@+id/appbar"
    app:elevation="12dp"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:titleTextColor="@color/white"
        android:elevation="12dp"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/Theme.AppCompat.NoActionBar"
        app:theme="@style/Jatt"
        app:layout_scrollFlags="scroll|enterAlways"/>

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_home" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@drawable/offline" />

</android.support.design.widget.CoordinatorLayout>

您已将其添加到acivity_home.xml

答案 1 :(得分:0)

@Override
public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.system_pref_sys_log);
}

在您的活动中尝试此操作。它对我有用。

还要查看是否在manifest.xml中为相关活动定义应用主题,然后将其删除并尝试。如果那么不需要遵循上面的代码。

答案 2 :(得分:0)

我有同样的错误,因为我在相对布局下添加了drawyerLayout。 因此,要解决该问题,我必须在RelativeLayout上指定上下文作为我的活动。

  

tools:mContext =“。activities.MainActivity”

<RelativeLayout 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:fillViewport="true"
tools:mContext=".activities.MainActivity">

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:openDrawer="start">
        ...
    </android.support.v4.widget.DrawerLayout>
</RelativeLayout>
...