LiveWallpaper:java.lang.IllegalStateException:表面已被释放已被、表面、java、LiveWallpaper

由网友(夜,靜得讓人悚然)分享简介:我创建了一个动态壁纸。I have created a Live Wallpaper.它工作正常,但是如果我想在屏幕上长preSS和我去住壁纸和preVIEW打开我的动态壁纸,在那之后的壁纸去破坏。It works fine, but if I want long press on the screen and...

我创建了一个动态壁纸。

I have created a Live Wallpaper.

它工作正常,但是如果我想在屏幕上长preSS和我去住壁纸和preVIEW打开我的动态壁纸,在那之后的壁纸去破坏。

It works fine, but if I want long press on the screen and I go to Live Wallpapers and open my Live Wallpaper in preview, after that the Wallpaper goes havoc.

我得到异常: java.lang.IllegalStateException:表面已经发布

推荐答案

这很难说没有你的code,但我看到这个异常,但只有当我导航离开了preVIEW之前,它是装载完毕。

It's hard to tell without your code, but I was seeing this exception, but only when I navigated away from the preview before it was finished loading.

在我的情况下,它被造成的,因为我开始了一个的AsyncTask onSurfaceCreated 方法关闭,但随后通过它得到的地方,我叫 surfaceHolder.lockCanvas()表面已经被摧毁了。

In my case, it was being caused because I started an AsyncTask off from the onSurfaceCreated method, but then by the time it got to the point where I called surfaceHolder.lockCanvas() the surface had already been destroyed.

要避开这个问题我推翻了 onSurfaceDestroyed 方法,并有一个变量全局的类名为 drawOk ,像这样的:

To get round this I overrode the onSurfaceDestroyed method, and had a variable global to that class called drawOk, like this:

    @Override
    public void onSurfaceCreated(SurfaceHolder holder) {
        super.onSurfaceCreated(holder);
        handler.post(reload);
        drawOk = true;
    }

    @Override
    public void onSurfaceDestroyed(SurfaceHolder holder) {
        super.onSurfaceDestroyed(holder);
        handler.removeCallbacks(reload);
        drawOk = false;
    }

    @Override
    public void onVisibilityChanged(boolean visible) {
        super.onVisibilityChanged(visible);
        if(visible) {
            handler.post(reload);
            drawOk = true;
        } else {
            handler.removeCallbacks(reload);
            drawOk = false;
        }
    }

    private void draw() {

        SurfaceHolder surfaceHolder = getSurfaceHolder();
        Canvas canvas = null;

        if(drawOk) {
            canvas = surfaceHolder.lockCanvas();
            if(canvas != null) {
                                // ...
            }
        }
    }   

有一个 surfaceHolder.isCreating(),而不是 surfaceHolder.isCreated()。这可能不是正确的方式做到这一点,但它是为我工作。

There is a surfaceHolder.isCreating(), but not a surfaceHolder.isCreated(). This might not be the right way to do it, but it is working for me.

阅读全文

相关推荐

最新文章