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.
You could try to set Exactly measurements explicitly always like below:
public class MyDrawerLayout extends DrawerLayout {
public MyDrawerLayout(@NonNull Context context) {
super(context);
}
public MyDrawerLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public MyDrawerLayout(@NonNull Context context, @Nullable 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);
}
}
Even I tried the Drawer's layout_height = "match_parent" but also java threw an error, so I just restarted android studio it worked no errors were found
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.
Make sure your activity is not a "Dialog" activity, check in the manifest. I made this mistake and got the error.
By mistake, I set a custom dialog theme to the activity instead of a custom activity theme:
activity.setTheme(R.style.StyledDialogTheme);
After i corrected this, it workes again.
For those who still have an exception, ensure your DrawerLayout
's height is match_parent