سؤال

I'm building a live wallpaper, it has a bitmap with fullHD resolution which is scaled down to the size of the users phone screen. It works perfectly on Android 2.2 and 2.3 but crashes on 4.0.

EDIT :

I narrowed the problem down a little bit using try/catch and found out it starts from my run() method. With that MacGyver fix it runs about 20 seconds flooding the following error message and then crashes:

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)

Here is my run() method:

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

EDIT 2 :

Could it have something to do with the emulator? I'm not able to access Android 4 device right now so that's what I have.

هل كانت مفيدة؟

المحلول

I'm still not sure why this happens with Android 4 but this is how I solved it.

This was causing me trouble:

class TestEngine extends Engine implements Runnable {}


So I changed it to something like this:

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);
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top