SurfaceView:dequeueBuffer在Android 4.0上失败

时间:2012-05-31 13:26:41

标签: android android-canvas live-wallpaper runnable surfaceholder

我正在构建一个动态壁纸,它有一个全高清分辨率的位图,缩小到用户手机屏幕的大小。它在Android 2.2和2.3上完美运行,但在4.0上崩溃。

编辑:

我使用try / catch缩小了问题,发现它从我的run()方法开始。使用MacGyver修复程序,它会运行大约20秒,然后崩溃以下错误消息然后崩溃:

05-31 20:01:22.590: E/SurfaceTextureClient(1475): dequeueBuffer failed (No such device)
05-31 20:01:22.590: E/BaseSurfaceHolder(1475): Exception locking surface
05-31 20:01:22.590: E/BaseSurfaceHolder(1475): java.lang.IllegalArgumentException
05-31 20:01:22.590: E/BaseSurfaceHolder(1475):  at android.view.Surface.lockCanvasNative(Native Method)
05-31 20:01:22.590: E/BaseSurfaceHolder(1475):  at android.view.Surface.lockCanvas(Surface.java:76)
05-31 20:01:22.590: E/BaseSurfaceHolder(1475):  at com.android.internal.view.BaseSurfaceHolder.internalLockCanvas(BaseSurfaceHolder.java:184)
05-31 20:01:22.590: E/BaseSurfaceHolder(1475):  at com.android.internal.view.BaseSurfaceHolder.lockCanvas(BaseSurfaceHolder.java:157)
05-31 20:01:22.590: E/BaseSurfaceHolder(1475):  at com.pelkkala.test.TestWall$TestMachine.run(TestWall.java:255)
05-31 20:01:22.590: E/BaseSurfaceHolder(1475):  at java.lang.Thread.run(Thread.java:856)
05-31 20:01:22.601: E/ERROR(1475): ERROR IN CODE: java.lang.NullPointerException
05-31 20:01:22.621: W/System.err(1475): java.lang.NullPointerException
05-31 20:01:22.621: W/System.err(1475):     at com.pelkkala.test.TestWall$TestMachine.draw(TestWall.java:233)
05-31 20:01:22.636: W/System.err(1475):     at com.pelkkala.test.TestWall$TestMachine.run(TestWall.java:264)
05-31 20:01:22.636: W/System.err(1475):     at java.lang.Thread.run(Thread.java:856)

这是我的run()方法:

        public void run() {

        while (running) {
            //perform canvas drawing
            if (!holder.getSurface().isValid()) {
                continue;
            }

            mTimer = System.currentTimeMillis();

            if (mTimer - oldTime > 50) {
                oldTime = mTimer;
                try {
                    Canvas c = holder.lockCanvas();

                    if (!touching) {
                        oscillate(xOrig, yOrig);
                    }
                    if (!touching2) {
                        oscillate2(xOrig2, yOrig2);
                    }

                    draw(c);        
                    holder.unlockCanvasAndPost(c);
                }
                catch (Exception e) {
                    Log.e("ERROR", "ERROR IN CODE: " + e.toString());
                    e.printStackTrace();
                }
            }
        }   
    }

编辑2:

它可能与模拟器有关吗?我现在无法访问Android 4设备,所以这就是我所拥有的。

1 个答案:

答案 0 :(得分:0)

我仍然不确定为什么Android 4会发生这种情况,但这就是我解决它的方法。

这给我带来了麻烦:

class TestEngine extends Engine implements Runnable {}


所以我把它改成了这样的东西:

class TestEngine extend Engine {

    private final Runnable testRunnable = new Runnable() {
        public void run() {
            drawFrame();
        }
    };

    public void drawFrame() {

        //Draw on canvas and unlock
        ...
        mHandler.postDelayed(testRunnable, 1000 / 50);
    }
}