2

I have a live wallpaper that is running fine. I wanted to add a settings activity to it, so I did all of the required steps and the settings button showed up when I went to the live wallpaper chooser. However, when I click on the settings button, I get the following exception.

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)

This is the code that's causing the exception which has been working fine until now (the line causing the exception has been indicated):

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

The sequences.get(i).draw() method is as follows

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

In my opinion this is the only relevant code, but if you want to see more I can post it here (please ask before downvoting). Also, the entire code is available on Github here.

gsgx
  • 12,020
  • 25
  • 98
  • 149

1 Answers1

6

I must have spent over 10 hours in the last few weeks just working on this issue, but I finally figured it out.

When the SettingsActivity is created, the wallpaper is no longer visible, but it still tries to draw on the surface. I changed the if statement at the bottom to wrap the entire draw function so I don't try to draw on the surface when it's not visible. That fixed my problem.

Hopefully this saves someone from wasting as much time as I did.

gsgx
  • 12,020
  • 25
  • 98
  • 149