In Robolectric, how do I get around DrawerLayout must be measured with MeasureSpec.EXACTLY error?

こ雲淡風輕ζ 提交于 2019-11-30 06:18:47

After some digging, I found a solution. It seems that Robolectric is somehow misscalculating the MeasureSpecs, which leads to this issue. In my case I could fix this problem by extending the DrawerLayout, and overriding onMeasure() :

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);
    }

}
Pavel Shkleinik

Update your Support Library jar: http://developer.android.com/tools/support-library/setup.html

And make sure that your project refers the latest one: How do I add a library (android-support-v7-appcompat) in IntelliJ IDEA

Example path to the lib: [adt-bundle-windows]\sdk\extras\android\support\v4\android-support-v4.jar

Just run your program on your android device. It runs perfectly. Just some problem in Eclipse i guess.

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