DrawerLayout must be measured with MeasureSpec.EXA

2019-01-12 05:29发布

I am trying to implement a Navigation drawer, but I keep getting this error. I saw the similar questions but did not work for me. I have the following layout activity_main2.xml:

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

   <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

   <RelativeLayout
        android:layout_gravity="left|start"
        android:layout_width="match_parent"
        android:background="#fff"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="#eee"
            android:background="#fff"
            android:dividerHeight="1dp" />
    </RelativeLayout>
</android.support.v4.widget.DrawerLayout>

On my activity_main2.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    initView();
    if (toolbar != null) {
        //toolbar.setTitle("");
        setSupportActionBar(toolbar);
    }
    initDrawer();
}

private void initView() {
    leftDrawerList = (ListView) findViewById(R.id.left_drawer);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    navigationDrawerAdapter = new ArrayAdapter<String>( MainActivity.this, android.R.layout.simple_list_item_1, leftSliderData);
    leftDrawerList.setAdapter(navigationDrawerAdapter);
    leftDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if(position == 1){
                drawerLayout.closeDrawer(Gravity.LEFT);
            }
        }
    });
}

private void initDrawer() {
    drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }
    };
    drawerLayout.setDrawerListener(drawerToggle);
}

Hopefully someone will help me with the right direction.

Many thanks in advance!

11条回答
迷人小祖宗
2楼-- · 2019-01-12 05:45

Make sure your activity is not a "Dialog" activity, check in the manifest. I made this mistake and got the error.

查看更多
女痞
3楼-- · 2019-01-12 05:46

This can also happen because of the new ConstraintLayout. Make sure the drawer layout isn't in the hierarchy of ConstraintLayout.

This is a really silly bug on Android's part. I will try to file a bug for it.

查看更多
Summer. ? 凉城
4楼-- · 2019-01-12 05:47

use this ...

public class CustomDrawerLayout extends DrawerLayout {
public CustomDrawerLayout(Context context) {
       super(context);
}
public CustomDrawerLayout(Context context, AttributeSet attrs) {
       super(context, attrs);
 }
     public CustomDrawerLayout(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);
    }
}
查看更多
Deceive 欺骗
5楼-- · 2019-01-12 05:51

This can also happen if you have set layout_width to wrap_content. In my case it was solved by setting width to match_parent. Hope this helps someone.

查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-01-12 05:59

Got to Build->Clean project then Rebuild project error will be solve

查看更多
姐就是有狂的资本
7楼-- · 2019-01-12 06:03

If you still get this Exception make sure that your activity is full Screen defined In Manifest u can do in Activity:

android:theme="@style/Theme.AppCompat.Light.FullScreen"

Or define this in styles.xml:

 <style name="Theme.AppCompat.Light.FullScreen" parent="@style/Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowNoTitle">false</item>
        <item name="android:windowActionBar">true</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>
查看更多
登录 后发表回答