android - SurfaceView onMeasure()没有通过调用setMeasuredDimension()来设置测量的维度

标签 android surfaceview onmeasure

我有一个surfaceview。我只是从另一个线程手动绘制。
它跑得很好。但在平均跑了10-20分钟后,我发现了一个例外:

01-14 08:51:25.000    3740-3740/com.myPackage E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalStateException: onMeasure() did not set the measured dimension by calling setMeasuredDimension()
            at android.view.View.measure(View.java:15200)
            at android.widget.RelativeLayout.measureChild(RelativeLayout.java:602)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415)
            at android.view.View.measure(View.java:15195)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4823)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:15195)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4823)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
            at android.view.View.measure(View.java:15195)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4823)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2340)
            at android.view.View.measure(View.java:15195)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4216)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
            at android.view.Choreographer.doCallbacks(Choreographer.java:555)
            at android.view.Choreographer.doFrame(Choreographer.java:525)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4813)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:559)
            at dalvik.system.NativeStart.main(Native Method)

安卓文档说:
测量视图及其内容以确定测量宽度和测量高度。此方法由measure(int,int)调用,并且应该由子类重写,以提供对其内容的准确有效的度量。
我认为google最初的实现是这样的:
@Override
    protected void More ...onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = getDefaultSize(mRequestedWidth, widthMeasureSpec);
        int height = getDefaultSize(mRequestedHeight, heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

我不知道我还能在这里实现什么。
我认为表面视图的大小永远不会改变。
注:
我的surfaceview包含在一个relativelayout中。
异常由子级调用,但它是一个视图(异常中的第3行)
如果你知道我如何解决这个讨厌的错误,请帮助我。
编辑:
在activity的oncreate中,我从代码中设置视图(扩展形式surfaceview)的大小
回答:
这是我如何为所需大小设置一些公共字段的方法:
Parameters.initialize(getResources().getDisplayMetrics().widthPixels,
                getResources().getDisplayMetrics().heightPixels);

参数。初始化:
public static void initialize(int screenWidth, int screenHeight){
    FIELD_WIDTH = screenWidth;
    FIELD_HEIGHT = (int)(0.92f * screenHeight);
}

myview是用xml编写的。
然后我在上面做了点什么:
public class MyViewManager {
    protected MyView mMyView;

    public FieldPainterComp(MainActivity activity) {
        super(activity);
        initialize();
    }

    private void initialize(){
        mMyView= (MyView) activity.findViewById(R.id.vMyView);
        mMyView.getLayoutParams().height = Parameters.FIELD_HEIGHT;
        mMyView.getLayoutParams().width = Parameters.FIELD_WIDTH;
    }
}

整个过程一开始就发生了。
android:screenOrientation="landscape"

屏幕永远不会变暗。游戏正在运行,只是有个例外。
我应该在什么地方叫量具吗?
我不明白为什么很久之后
必须与此相关的其他异常:
01-19 11:53:39.812  29666-31881/com.mypackage V/GAV4﹕ Thread[disconnect check,5,main]: Disconnecting due to inactivity
01-19 11:53:39.812  29666-31881/com.mypackage V/GAV4﹕ Thread[disconnect check,5,main]: Disconnected from service
01-19 11:57:17.732  29666-29666/com.mypackage D/AndroidRuntime﹕ Shutting down VM
01-19 11:57:17.732  29666-29666/com.mypackage W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x420face0)
01-19 11:57:17.812  29666-29666/com.mypackage V/GAV4﹕ Thread[main,5,main]: Tracking Exception: IllegalStateException (@View:measure:16533) {main}
01-19 11:57:17.832  29666-29666/com.mypackage V/GAV4﹕ Thread[main,5,main]: Dispatch call queued. Dispatch will run once initialization is complete.
01-19 12:19:27.982  29666-29670/com.mypackage I/dalvikvm﹕ threadid=3: reacting to signal 3
01-19 12:19:28.262  29666-29670/com.mypackage I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
01-19 12:19:32.452  29666-29666/com.mypackage A/libc﹕ Fatal signal 6 (SIGABRT) at 0x0000025a (code=0), thread 29666 (ames.adaptation)

GAV是谷歌的分析工具。
我提出了另一个关于anr的问题:ANR with 3 threads - using locks

最佳答案

setMeasuredDimension()

必须在onMeasure()方法内部调用。我遇到了一个类似的问题,逻辑没有到达setMeasuredDimension()。我解决了强制调用setMeasuredDimension()的问题,不管前面的if/else语句是什么。

本文翻译自 https://stackoverflow.com/questions/28020352/

网站遵循 CC BY-SA 4.0 协议,转载或引用请注明出处。


相关文章:

android - 使用通用图像加载器下载的图像设置RelativeLayout的背景

android - 测试运行失败:无法找到以下组件的检测信息:

java - Android SurfaceView闪烁

java - 在Android中绘制动态自定义视图国际象棋棋盘

android - onMeasure被称为1500多次

android - 在Android 7(API级别24)中,我的应用程序不允许静音手机(将铃声模式设置为静音)

android - Android SurfaceView滚动

android - 一些设备上Surface.nativeLockCanvas上的ANR

android - 如何在Android中平稳地实现放大缩小?与带有RTMP流的SurfaceView相关

android - 如何在点击时突出显示表格行?