Search code examples
androidlive-wallpaperillegalstateexceptionsurfaceholder

SurfaceHolder : IllegalStateException - Surface was not locked


I am getting an error in my program. The error seems to state that my surface isn't locked when I try to unlock it at the end my draw sequence. But as shown in the code below. I do lock the the surface first thing. Why am I getting this error. What is it that I am doing wrong?

The method that is giving the error:

error line '**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)

Solution

  • You are attempting to unlock twice. Remove the else block from case 1.