运行时DrawerLayout必须MeasureSpec.EXACTLY excpetion进行测量 [英] Runtime DrawerLayout must be measured with MeasureSpec.EXACTLY excpetion

查看:5858
本文介绍了运行时DrawerLayout必须MeasureSpec.EXACTLY excpetion进行测量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面教程的,我面临着一个奇怪的问题。

I'm following this tutorial from and I'm facing with a strange problem.

没关系,什么是我的 DrawerLayout ,但如果它的 layout_height layout_width 设置为任何东西比硬codeD编号等,它抛出此运行时异常。

It doesn't matter what's in my DrawerLayout, but if its layout_height or layout_width are set to anything other than hardcoded number, it throws this runtime exception.

我已经看到很多这个错误试图呈现的图形布局工具的输出时,抛出的报告,但找不到任何一个运行时错误。

I've seeing plenty of reports of this error thrown when trying to render an output in the Graphical Layout tool, but couldn't find any on a runtime error.

我有最新的支持库,更新的只是现在版本 21.0.2

I have the latest support library, updated just now to version 21.0.2

我想避免硬编码我的高度和宽度,如果可能的话。

I would like to avoid hardcoding my height and width if possible.

该文件的开头是这样的。这样一来,压伤。

The beginning of the file looks like this. This way, it crushes.

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

和堆栈跟踪是

12-04 00:57:51.051: E/AndroidRuntime(26254): FATAL EXCEPTION: main
12-04 00:57:51.051: E/AndroidRuntime(26254): Process: com.lablabla.homedestroyer, PID: 26254
12-04 00:57:51.051: E/AndroidRuntime(26254): java.lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY.
12-04 00:57:51.051: E/AndroidRuntime(26254):    at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:814)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at android.view.View.measure(View.java:17430)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at android.view.View.measure(View.java:17430)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at android.view.View.measure(View.java:17430)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at android.view.View.measure(View.java:17430)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1141)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at android.view.Choreographer.doCallbacks(Choreographer.java:580)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at android.view.Choreographer.doFrame(Choreographer.java:550)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at android.os.Handler.handleCallback(Handler.java:739)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at android.os.Handler.dispatchMessage(Handler.java:95)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at android.os.Looper.loop(Looper.java:135)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at android.app.ActivityThread.main(ActivityThread.java:5221)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at java.lang.reflect.Method.invoke(Native Method)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at java.lang.reflect.Method.invoke(Method.java:372)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
12-04 00:57:51.051: E/AndroidRuntime(26254):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

如果它必须是硬codeD,什么是使正确的各种屏幕尺寸的最佳方法是什么?

If it has to be hardcoded, what's the best way to make it "right" for various screen sizes?

推荐答案

尝试覆盖DrawerLayout对测量方法

Try overriding the DrawerLayout's on measure method

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

}

这篇关于运行时DrawerLayout必须MeasureSpec.EXACTLY excpetion进行测量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆