SurfaceHolder:IllegalStateException - Surface未锁定

时间:2014-05-25 23:21:15

标签: android live-wallpaper illegalstateexception surfaceholder

我的程序出错了。该错误似乎表明当我尝试在绘制序列的最后解锁时,我的表面没有被锁定。但如下面的代码所示。我首先锁定了表面。为什么我收到此错误。我做错了什么?

给出错误的方法:

错误行' **this.holder.unlockCanvasAndPost(canvas);**'

public void run() {
        while (this.drawFlag) {
            Canvas canvas = this.holder.lockCanvas();
            canvas.drawColor(Color.rgb(168, 201, 187));
            switch (this.sceneFlag) {
            case -1:
                draw(canvas);
                drawHow(canvas);
                drawStr(canvas);
                break;
            case 0:
                draw(canvas);
                drawStr(canvas);
                break;
            case 1:
                update();
                draw(canvas);
                if (this.timeAttackFlag && this.optionNum == 0) {
                    this.sceneFlag = 2;
                    try {
                        setScore();
                    } catch (Exception e) {
                    }
                } else {
                    this.holder.unlockCanvasAndPost(canvas);
                }
                break;
            case 2:
                drawResult(canvas);
                break;
            }
            this.holder.unlockCanvasAndPost(canvas);
            try {
                TimeUnit.NANOSECONDS.sleep(this.fpsM.state());
            } catch (Exception e_2) {
            }
        }
    }

Logcat:

05-25 19:12:14.091: E/AndroidRuntime(21305): FATAL EXCEPTION: Thread-6886
05-25 19:12:14.091: E/AndroidRuntime(21305): Process: com.gordondev.smackitup, PID: 21305
05-25 19:12:14.091: E/AndroidRuntime(21305): java.lang.IllegalStateException: Surface was not locked
05-25 19:12:14.091: E/AndroidRuntime(21305):    at android.view.Surface.unlockCanvasAndPost(Surface.java:268)
05-25 19:12:14.091: E/AndroidRuntime(21305):    at android.view.SurfaceView$4.unlockCanvasAndPost(SurfaceView.java:852)
05-25 19:12:14.091: E/AndroidRuntime(21305):    at com.gordondev.smackitup.GameView.run(GameView.java:858)
05-25 19:12:14.091: E/AndroidRuntime(21305):    at java.lang.Thread.run(Thread.java:841)

1 个答案:

答案 0 :(得分:1)

您正尝试解锁两次。从案例1中删除else块。