4

I'm trying to create my own livewallpaper for Android. I have starting project that compiles and opens settings Activity. But after clicking button for select wallpaper picker crashes and appears following exception:

FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:299)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    at java.lang.Thread.run(Thread.java:838)
Caused by: java.lang.ClassCastException: android.graphics.drawable.LayerDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
    at com.android.wallpaper.livepicker.LiveWallpaperListAdapter$LiveWallpaperEnumerator.doInBackground(LiveWallpaperListAdapter.java:226)
    at com.android.wallpaper.livepicker.LiveWallpaperListAdapter$LiveWallpaperEnumerator.doInBackground(LiveWallpaperListAdapter.java:149)
    at android.os.AsyncTask$2.call(AsyncTask.java:287)
    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
    at java.lang.Thread.run(Thread.java:838) 

It appears also if open wallpaper picker from phone settings. Can my wallpaper be the reason of error? And how? I don't use any drawable at the moment.

Added

Code for open wallpaper picker:

Intent intent = new Intent(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER);
intent.putExtra(WallpaperManager.WALLPAPER_PREVIEW_META_DATA,
                new ComponentName(SettingsActivity.this, MyWallpaperService.class));
startActivity(intent);

And MyWallpaperService class code:

public class MyWallpaperService extends WallpaperService {

    @Override
    public Engine onCreateEngine() {
        return new MyEngine();
    }

    private class MyEngine extends Engine {
        private boolean visible;
        private Handler handler;
        private Runnable drawRunner = new Runnable() {
            @Override
            public void run() {
                draw();
            }
        };
        private int width;
        private int height;
        private BallsContainer balls;

        public MyEngine() {
            balls = new BallsContainer();
        }

        @Override
        public void onVisibilityChanged(boolean visible) {
          this.visible = visible;
          if (visible) {
            handler.post(drawRunner);
          } else {
            handler.removeCallbacks(drawRunner);
          }
        }

        @Override
        public void onSurfaceDestroyed(SurfaceHolder holder) {
          super.onSurfaceDestroyed(holder);
          this.visible = false;
          handler.removeCallbacks(drawRunner);
        }

        @Override
        public void onSurfaceChanged(SurfaceHolder holder, int format,
            int width, int height) {
          this.width = width;
          this.height = height;
          super.onSurfaceChanged(holder, format, width, height);
        }

        private void draw() {
            SurfaceHolder holder = getSurfaceHolder();
            Canvas canvas = null;
            try {
              canvas = holder.lockCanvas();
              if (canvas != null) {
                  balls.Draw(canvas, width, height);
              }
            } finally {
              if (canvas != null)
                holder.unlockCanvasAndPost(canvas);
            }
            handler.removeCallbacks(drawRunner);
            if (visible) {
              handler.postDelayed(drawRunner, 100);
            }
          }
    }
}

Added one more time

BallsContainer draw method:

public void Draw(Canvas canvas, int width, int height) {
    Iterator<Ball> iter = balls.iterator();
    while(iter.hasNext()) {
        Ball b = iter.next();
        b.Move(width, height);
        b.Draw(canvas);
    }
}

And Ball draw method:

public void Draw(Canvas canvas) {
    canvas.drawCircle(pos.x, pos.y, radius, paint );
}
Ircover
  • 2,406
  • 2
  • 22
  • 42

2 Answers2

1

AsyncTask's doInBackground() throws ClassCastException because it try to cast LayerDrawable to BitmapDrawable. Provide Drawable instance that can be cast to BitmapDrawable safely.

Zamrony P. Juhara
  • 5,222
  • 2
  • 24
  • 40
0

The only advice from Phillips support was to make factory reset. I made it - there is no more crash. I don't like this solution, but it works and I haven't a better one.

Ircover
  • 2,406
  • 2
  • 22
  • 42