2017-01-22 56 views
0

來測量我已用導航抽屜和幀佈局的活性。我想在底部顯示一個TextView。我已將FrameLayout和TextView放入LinearLayout中。只要我運行這個活動,我就會收到這個錯誤。添加的TextView後:java.lang.IllegalArgumentException異常:DrawerLayout必須MeasureSpec.EXACTLY

這裏的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> 

回答

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

+0

沒有工作。是的,DrawerLayout的高度是match_parent,你可以在主帖中的代碼中看到。 –

+0

@ParasSidhu檢查編輯答案 – rafsanahmad007

+0

我刪除了DrawerLayout標籤,但具有同樣的錯誤:( –

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

在您的活動中嘗試此操作。這個對我有用。

也看看你是否在manifest.xml中的相關活動定義應用的主題,如果然後將其刪除,並嘗試它。如果不需要遵循上面的代碼。

相關問題