Android动态壁纸不显示 [英] Android Live wallpaper doesn't show

查看:269
本文介绍了Android动态壁纸不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习动态壁纸和我做了这个小东西。

事情是我的应用程序编译与出任何问题。

我在手机打开它显示在preVIEW墙纸但是当我点击设置为墙纸它以某种方式崩溃,并返回前屏幕,我的壁纸将变成黑色,但是它没有运行它。

我的猜测是对的问题是一些地方沿onDestory()。

 公共类扩展条{WallpaperService私人StripsEngine引擎;@覆盖
公共无效的onCreate(){
    super.onCreate();
    发动机=新StripsEngine();
    engine.resume();}@覆盖
公共无效的onDestroy(){
    super.onDestroy();
    engine.pause();
}@覆盖
公共引擎onCreateEngine(){    返回引擎;
}类StripsEngine扩展引擎实现Runnable {
    私人线程t = NULL;
    私人SurfaceHolder持有人;
    私人布尔clearToRun = FALSE;
    民营涂料粉刷=新的油漆();
    私人油漆paint2 =新的油漆();
    私人诠释框架;
    私人浮动endX,恩迪;
    私人诠释高度,宽度;
    私人帆布℃;
    StripsEngine(){
        支架= getSurfaceHolder();
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(30);
        paint.setAntiAlias​​(真);
        paint.setStrokeCap​​(Paint.Cap.ROUND);
        paint.setStyle(Paint.Style.STROKE);
        paint2 =漆;
        paint2.setColor(Color.BLACK);
        paint2.setStrokeWidth(40);
        帧= 0;
    }    无效drawStrips(){    }    公共无效的run(){
        // TODO自动生成方法存根
        而(clearToRun){
            如果(!holder.getSurface()。的isValid()){
                继续;
            }
            高度= 800;
            宽= 480;            endX =(帧)%(宽+ 10);
            恩迪=(帧/ 3)%(高+ 10);            如果(endX == 0)
                paint.setARGB(255,(int)的(的Math.random()* 255),(INT)(的Math.random()* 255),(INT)(的Math.random()* 255));
            如果(恩迪== 0)
                paint.setARGB(255,(int)的(的Math.random()* 255),(INT)(的Math.random()* 255),(INT)(的Math.random()* 255));
            C = holder.lockCanvas();
            c.drawLine(endX-30,恩迪-10,endX,恩迪,油漆);
            c.drawLine(endX-30,恩迪-10,endX,恩迪,油漆);
            holder.unlockCanvasAndPost(C);
            帧=帧+2;
            }
    }
    公共无效暂停(){
        clearToRun = FALSE;
        而(真){
            尝试{
                t.join();
            }
            赶上(InterruptedException的E){
                e.printStackTrace();
            }
            打破;
        }
        T = NULL;
    }    公共无效简历(){
        clearToRun =真;
        T =新的Thread(本);        t.start();
    }
}}


  

07-06 17:14:43.869:E /面(478):错误出列一个缓冲区(未知的错误:-19)
  07-06 17:14:43.869:E /面(478):dequeueBuffer失败(没有这样的设备)
  07-06 17:14:43.869:E / BaseSurfaceHolder(478):异常锁定面
  07-06 17:14:43.869:E / BaseSurfaceHolder(478):java.lang.IllegalArgumentException异常
  07-06 17:14:43.869:E / BaseSurfaceHolder(478):在android.view.Surface.lockCanvasNative(本机方法)
  07-06 17:14:43.869:E / BaseSurfaceHolder(478):在android.view.Surface.lockCanvas(Surface.java:288)
  07-06 17:14:43.869:E / BaseSurfaceHolder(478):在com.android.internal.view.BaseSurfaceHolder.internalLockCanvas(BaseSurfaceHolder.java:132)
  07-06 17:14:43.869:E / BaseSurfaceHolder(478):在com.android.internal.view.BaseSurfaceHolder.lockCanvas(BaseSurfaceHolder.java:112)
  07-06 17:14:43.869:E / BaseSurfaceHolder(478):在com.mendel.strips.Strips $ StripsEngine.run(Strips.java:81)
  07-06 17:14:43.869:E / BaseSurfaceHolder(478):在java.lang.Thread.run(Thread.java:1096)
  07-06 17:14:43.972:W / dalvikvm(478):主题ID = 15:螺纹未捕获的异常退出(组= 0x4001b188)
  07-06 17:14:43.979:E / AndroidRuntime(478):未捕获的处理程序:螺纹螺纹-8,由于未捕获的异常退出
  07-06 17:14:43.990:E / AndroidRuntime(478):显示java.lang.NullPointerException
  07-06 17:14:43.990:E / AndroidRuntime(478):在com.mendel.strips.Strips $ StripsEngine.run(Strips.java:82)
  07-06 17:14:43.990:E / AndroidRuntime(478):在java.lang.Thread.run(Thread.java:1096)
  07-06 17:14:43.999:I / dalvikvm(478):主题ID = 7:反应信号3
  07-06 17:14:43.999:E / dalvikvm(478):无法打开堆栈跟踪文件'/data/anr/traces.txt:不准



解决方案

这可能是因为你与 holder.lockCanvas请求()画布上还没有提供,所以它为空,你会得到一个NullPointerException异常。

请参阅的文件 lockCanvas()


  

返回的画布可以用来绘制成面的位图。如果表面没有被创建或返回空,否则无法进行编辑。您通常需要实现Callback.surfaceCreated找出当表面可供使用。


更新:

据给isValid()的文档,您发出呼叫前 - 这是否对象持有有效的表面?如果持有的物理表面返回true,所以lockCanvas()将会成功。否则返回false。

画布不应该在这一点空。但我发现这一点,似乎是Android的一个错误:

HTTP://$c$c.google。 COM / p /安卓/问题/细节?ID = 19245

所以,你可能要尝试一下 Callback.surfaceCreated 的办法,或者,如果没有其他作品,尝试像海报超​​时(谁删除的帖子),建议,但是这真的是你应该避免,因为它是不可靠的,凌乱的。

i have just started learning live wallpapers and i made this little thing.

the thing is my app compiles with out any problems.

as i open it in the phone it shows the wallpaper in the preview but when i click "set as wallpaper" it somehow crashes, and returns the front screen, and my wallpaper turns black, but it doesn't run it.

my guess is the problem is some where along the onDestory() ..

public class Strips extends WallpaperService {

private StripsEngine engine;

@Override
public void onCreate() {
    super.onCreate();
    engine = new StripsEngine();
    engine.resume();

}

@Override
public void onDestroy() {
    super.onDestroy();
    engine.pause();
}

@Override
public Engine onCreateEngine() {

    return engine;
}

class StripsEngine extends Engine implements Runnable {
    private Thread t = null;
    private SurfaceHolder holder;
    private boolean clearToRun = false;
    private Paint paint = new Paint();
    private Paint paint2 = new Paint();
    private int frame;
    private float endX,endY;
    private int height, width;
    private Canvas c;


    StripsEngine() {
        holder = getSurfaceHolder();
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(30);
        paint.setAntiAlias(true);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStyle(Paint.Style.STROKE);
        paint2 = paint;
        paint2.setColor(Color.BLACK);
        paint2.setStrokeWidth(40);
        frame = 0;
    }

    void drawStrips() {

    }

    public void run() {
        // TODO Auto-generated method stub
        while (clearToRun) {
            if (!holder.getSurface().isValid()) {
                continue;
            }
            height = 800;
            width = 480; 

            endX = (frame) % (width + 10);
            endY = (frame / 3) % (height + 10);

            if (endX == 0)
                paint.setARGB(255,(int)(Math.random()*255), (int)(Math.random()*255),(int)(Math.random()*255));
            if (endY ==0)
                paint.setARGB(255,(int)(Math.random()*255), (int)(Math.random()*255),(int)(Math.random()*255));


            c = holder.lockCanvas();
            c.drawLine(endX-30, endY-10, endX , endY,paint);
            c.drawLine(endX-30, endY-10, endX , endY,paint);
            holder.unlockCanvasAndPost(c);
            frame = frame +2;
            }
    }
    public void pause() {
        clearToRun = false;
        while (true) {
            try {
                t.join();
            }
            catch (InterruptedException e) {
                e.printStackTrace();    
            }
            break;
        }
        t = null;
    }

    public void resume() {
        clearToRun = true;
        t = new Thread(this);

        t.start();
    }
}

}

07-06 17:14:43.869: E/Surface(478): error dequeuing a buffer (Unknown error: -19) 07-06 17:14:43.869: E/Surface(478): dequeueBuffer failed (No such device) 07-06 17:14:43.869: E/BaseSurfaceHolder(478): Exception locking surface 07-06 17:14:43.869: E/BaseSurfaceHolder(478): java.lang.IllegalArgumentException 07-06 17:14:43.869: E/BaseSurfaceHolder(478): at android.view.Surface.lockCanvasNative(Native Method) 07-06 17:14:43.869: E/BaseSurfaceHolder(478): at android.view.Surface.lockCanvas(Surface.java:288) 07-06 17:14:43.869: E/BaseSurfaceHolder(478): at com.android.internal.view.BaseSurfaceHolder.internalLockCanvas(BaseSurfaceHolder.java:132) 07-06 17:14:43.869: E/BaseSurfaceHolder(478): at com.android.internal.view.BaseSurfaceHolder.lockCanvas(BaseSurfaceHolder.java:112) 07-06 17:14:43.869: E/BaseSurfaceHolder(478): at com.mendel.strips.Strips$StripsEngine.run(Strips.java:81) 07-06 17:14:43.869: E/BaseSurfaceHolder(478): at java.lang.Thread.run(Thread.java:1096) 07-06 17:14:43.972: W/dalvikvm(478): threadid=15: thread exiting with uncaught exception (group=0x4001b188) 07-06 17:14:43.979: E/AndroidRuntime(478): Uncaught handler: thread Thread-8 exiting due to uncaught exception 07-06 17:14:43.990: E/AndroidRuntime(478): java.lang.NullPointerException 07-06 17:14:43.990: E/AndroidRuntime(478): at com.mendel.strips.Strips$StripsEngine.run(Strips.java:82) 07-06 17:14:43.990: E/AndroidRuntime(478): at java.lang.Thread.run(Thread.java:1096) 07-06 17:14:43.999: I/dalvikvm(478): threadid=7: reacting to signal 3 07-06 17:14:43.999: E/dalvikvm(478): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

解决方案

It's probably because the canvas you are requesting with holder.lockCanvas() is not available yet, so it's null, and you get a NullPointerException.

See the documentation of lockCanvas()

The returned Canvas can be used to draw into the surface's bitmap. A null is returned if the surface has not been created or otherwise can not be edited. You will usually need to implement Callback.surfaceCreated to find out when the Surface is available for use.

Update:

According to the doc of isValid(), which you are calling before - Does this object hold a valid surface? Returns true if it holds a physical surface, so lockCanvas() will succeed. Otherwise returns false.

The canvas should not be null at that point. But I found this, seems to be a bug of Android:

http://code.google.com/p/android/issues/detail?id=19245

So you probably have to try out the Callback.surfaceCreated approach, or, if nothing else works, try a timeout like the poster (who deleted the post), suggested, but that's really something you should avoid because it's unreliable and messy.

这篇关于Android动态壁纸不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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