LiveWallpaper:java.lang.IllegalStateException:表面已被释放 [英] LiveWallpaper: java.lang.IllegalStateException: Surface has already been released

查看:1229
本文介绍了LiveWallpaper:java.lang.IllegalStateException:表面已被释放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个动态壁纸。

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.

这篇关于LiveWallpaper:java.lang.IllegalStateException:表面已被释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆