Android 动态壁纸选择器崩溃

Android LiveWallpaper picker crash

我正在尝试为 Android 创建我自己的动态壁纸。我有一个编译和打开设置的启动项目 Activity。但是在单击 select 壁纸选择器按钮后崩溃并出现以下异常:

FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
    at android.os.AsyncTask.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.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.call(AsyncTask.java:287)
    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
    at android.os.AsyncTask$SerialExecutor.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) 

如果从 phone 设置中打开墙纸选择器,它也会出现。 我的墙纸可能是错误的原因吗?如何?我目前不使用任何可绘制对象。

已添加

打开壁纸选择器的代码:

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

MyWallpaperService class代码:

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

又加了一次

BallsContainer绘制方法:

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

Ball绘制方法:

public void Draw(Canvas canvas) {
    canvas.drawCircle(pos.x, pos.y, radius, paint );
}

AsyncTaskdoInBackground() 抛出 ClassCastException 因为它试图将 LayerDrawable 转换为 BitmapDrawable。提供可以安全转换为 BitmapDrawable 的 Drawable 实例。

Phillips 支持人员的唯一建议是恢复出厂设置。我做到了 - 不再有崩溃。我不喜欢这个解决方案,但它确实有效而且我没有更好的解决方案。