2

我有一个运行良好的动态壁纸。我想为其添加一个设置活动,所以我完成了所有必需的步骤,当我进入动态壁纸选择器时,设置按钮出现了。但是,当我单击设置按钮时,出现以下异常。

11-23 16:12:53.158: E/AndroidRuntime(5141): FATAL EXCEPTION: main
11-23 16:12:53.158: E/AndroidRuntime(5141): java.lang.IllegalArgumentException
11-23 16:12:53.158: E/AndroidRuntime(5141):     at android.view.Surface.nativeUnlockCanvasAndPost(Native Method)
11-23 16:12:53.158: E/AndroidRuntime(5141):     at android.view.Surface.unlockCanvasAndPost(Surface.java:457)
11-23 16:12:53.158: E/AndroidRuntime(5141):     at com.android.internal.view.BaseSurfaceHolder.unlockCanvasAndPost(BaseSurfaceHolder.java:215)
11-23 16:12:53.158: E/AndroidRuntime(5141):     at com.gulshansingh.hackerlivewallpaper.HackerWallpaperService$HackerWallpaperEngine.draw(HackerWallpaperService.java:48)
11-23 16:12:53.158: E/AndroidRuntime(5141):     at com.gulshansingh.hackerlivewallpaper.HackerWallpaperService$HackerWallpaperEngine.access$0(HackerWallpaperService.java:36)
11-23 16:12:53.158: E/AndroidRuntime(5141):     at com.gulshansingh.hackerlivewallpaper.HackerWallpaperService$HackerWallpaperEngine$1.run(HackerWallpaperService.java:31)
11-23 16:12:53.158: E/AndroidRuntime(5141):     at android.os.Handler.handleCallback(Handler.java:725)
11-23 16:12:53.158: E/AndroidRuntime(5141):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-23 16:12:53.158: E/AndroidRuntime(5141):     at android.os.Looper.loop(Looper.java:137)
11-23 16:12:53.158: E/AndroidRuntime(5141):     at android.app.ActivityThread.main(ActivityThread.java:5039)
11-23 16:12:53.158: E/AndroidRuntime(5141):     at java.lang.reflect.Method.invokeNative(Native Method)
11-23 16:12:53.158: E/AndroidRuntime(5141):     at java.lang.reflect.Method.invoke(Method.java:511)
11-23 16:12:53.158: E/AndroidRuntime(5141):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-23 16:12:53.158: E/AndroidRuntime(5141):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-23 16:12:53.158: E/AndroidRuntime(5141):     at dalvik.system.NativeStart.main(Native Method)

这是导致异常的代码,到目前为止一直运行良好(已指示导致异常的行):

    /** Draws all of the bit sequences on the screen */
    private void draw() {
        SurfaceHolder holder = getSurfaceHolder();
        Canvas c = holder.lockCanvas();
        if (c != null) {
            try {
                c.drawARGB(255, 0, 0, 0);

                for (int i = 0; i < sequences.size(); i++) {
                    sequences.get(i).draw(c);
                }

            } finally {
                holder.unlockCanvasAndPost(c); // line 48
            }
        }

        // Remove the runnable, and only schedule the next run if visible
        handler.removeCallbacks(drawRunnable);
        if (visible) {
            handler.post(drawRunnable);
        } else {
            stop();
        }
    }

方法sequences.get(i).draw()如下

synchronized public void draw(Canvas canvas) {
    paint.setAlpha(0);
    float prevY = y;
    for (int i = 0; i < bits.size(); i++) {
        canvas.drawText(bits.get(i), x, y, paint);
        y += TEXT_SIZE;
        paint.setAlpha(paint.getAlpha() + INCREMENT);
    }
    y = prevY;
}

在我看来,这是唯一相关的代码,但如果你想看到更多我可以在这里发布(请在投票前询问)。此外,完整的代码可以在 Github 上找到

4

1 回答 1

6

在过去的几周里,我一定花了 10 多个小时来解决这个问题,但我终于弄明白了。

创建 SettingsActivity 时,壁纸不再可见,但仍会尝试在表面上绘制。我更改了底部的 if 语句以包装整个绘图函数,因此当表面不可见时,我不会尝试在表面上绘图。这解决了我的问题。

希望这可以避免有人像我一样浪费时间。

于 2012-12-08T01:39:45.373 回答