Illegal Argument Exception - DrawerLayout must be measured with MeasureSpec.EXACTLY

半世苍凉 提交于 2019-12-02 02:59:08

Error:

Illegal Argument Exception - DrawerLayout must be measured with MeasureSpec.EXACTLY

From error log i can suggest to create custom class that extends DrawerLayout and forcing the correct Measure_Specs:

public class MyDrawerLayout extends DrawerLayout {

    public MyDrawerLayout(Context context) {
        super(context);
    }

    public MyDrawerLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        widthMeasureSpec = MeasureSpec.makeMeasureSpec(
                MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY);
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(
                MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

}

or As a temporary workaround try to set layout_width and/or layout_height to a specific size (e.g. 500dp) instead of match_parent.

EDIT:

BaseActivity.java

import com.steve.database.MyDrawerLayout;

private MyDrawerLayout mDrawerLayout;
private ListView mDrawerList;


LayoutInflater mInflater = (LayoutInflater) getApplicationContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);

mDrawerLayout = (MyDrawerLayout)mCustomView.findViewById(R.id.drawer_layout);
mDrawerList = (ListView)mCustomView.findViewById(R.id.list_menu);

I tried lots of solution nothing worked.After a long time i found a solution by my self.

i was using Linear Layout as a parent of my inflating class i just replaced with Relative Layout

after that everything is working fine.For your better understanding given below is my code that i am using currently for inflating class

FrameLayout layoutContainer = (FrameLayout ) findViewById(R.id.container);
    LayoutInflater layoutInflater = (LayoutInflater)
            this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 layoutContainer.addView(layoutInflater.inflate(R.layout.ecom_home_activity, null));

And ecom_home_activity is inflating class.The code of is given below.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/gray_background_color"
android:fillViewport="true"
android:orientation="vertical"
android:scrollbars="none">

<RelativeLayout
    android:id="@+id/no_data_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#cc000000"
    android:gravity="center">

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:indeterminate="true"
        android:indeterminateTint="@color/offer_activity_background"
        android:indeterminateTintMode="src_atop"
        tools:ignore="UnusedAttribute" />

    <TextView
        android:id="@+id/noDataHeading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/progressBar"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:text="@string/offerlist_please_wait"
        android:textColor="@color/white_color"
        android:textSize="@dimen/twenty_six_text_size" />

    <TextView
        android:id="@+id/noDataText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/noDataHeading"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:text="@string/offerlist_while_we_are"
        android:textColor="@color/white_color"
        android:textSize="@dimen/fourteen_size_text" />
</RelativeLayout>

<ImageView
    android:id="@+id/sleeping_bird"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:scaleType="centerInside" />

<TextView
    android:id="@+id/textView_taking_nap"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/sleeping_bird"
    android:layout_marginTop="@dimen/five_dp_margin_padding"
    android:gravity="center"
    android:text="@string/offerlist_coucou_taking_nap"
    android:textSize="@dimen/sixteen_size_text_heading"
    android:visibility="gone" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</LinearLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerListOfferProducts"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:footerDividersEnabled="false"
    android:gravity="center"
    android:headerDividersEnabled="false"
    android:paddingBottom="@dimen/five_dp_margin_padding"
    android:paddingLeft="@dimen/eight_dp_margin_padding"
    android:paddingRight="@dimen/eight_dp_margin_padding"
    android:paddingTop="@dimen/eight_dp_margin_padding"
    android:scrollbars="none" />

Previously i was using Linear layout of ecom_home_activity class i just changed Linear layout with Relative layout

After that everything is working fine.In your case if you are using Linear Layout of your inflating class then Replace with Relative Layout.hope my answer will help you cheers.

set layout_width and layout_height to specific size, not match_parent or fill_parent.

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".UserListActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:background="@drawable/common_gradient"
    android:layoutDirection="rtl"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.2">

        <TextView
            android:id="@+id/userType_textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="نوع المستخدم"
            android:textColor="#000000"
            android:textSize="20sp"
            tools:text="نوع المستخدم" />

        <TextView
            android:id="@+id/className_textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/userType_textView"
            android:layout_centerHorizontal="true"
            android:text="إسم القسم"
            android:textColor="#000000"
            android:textSize="16sp"
            tools:text="إسم القسم" />

        <ImageButton
            android:layout_width="30dp"
            android:layout_height="20dp"
            android:layout_alignBottom="@+id/userType_textView"
            android:layout_marginLeft="15dp"
            android:layout_marginStart="15dp"
            android:background="@android:color/transparent"
            android:contentDescription="@string/desc"
            android:onClick="showMenuAction"
            android:scaleType="fitCenter"
            android:src="@drawable/menu" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.8"

        android:background="#FAFAFA">

        <SearchView
            android:id="@+id/user_searchView"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:background="#9CC3D7" />

        <ListView
            android:id="@+id/users_listView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            android:layout_alignParentBottom="true"
            android:layout_below="@+id/user_searchView"
            android:layout_centerHorizontal="true"
            android:divider="#DFDEE1"
            android:dividerHeight="1dp" />
    </RelativeLayout>

</LinearLayout>

<android.support.v4.widget.DrawerLayout
    android:id="@+id/navigationDrawerUser"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:fitsSystemWindows="true"
    android:layoutDirection="rtl">


    <ExpandableListView
        android:id="@+id/menu_listView_user"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#195269"
        android:choiceMode="singleChoice"
        android:divider="#2C637D"
        android:dividerHeight="1dp"
        android:groupIndicator="@null">

    </ExpandableListView>

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

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