2016-02-17 180 views
4

我正在嘗試爲Android創建我自己的動態壁紙。我已經開始編譯並打開設置Activity的項目。但是,對於點擊選擇壁紙選擇崩潰按鈕後出現以下異常:Android LiveWallpaper選取器崩潰

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)  

它也出現在從手機設置開放的壁紙選擇。 我的壁紙可以成爲錯誤的原因嗎?如何?我目前不使用任何drawable。

新增

代碼開放的壁紙選擇:

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類代碼:

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

@ cricket_007'LiveWallpaperListAdapter'不是我的課 - 它是Android。 – Ircover

+0

啊,現在看。儘管如此,請添加任何代碼在您的AsyncTask –

+0

@ cricket_007我沒有'AsyncTask's - 它都在Androids壁紙選擇器內。 – Ircover

回答

0

Phillips支持的唯一建議是使工廠重置。我做到了 - 沒有更多的崩潰。我不喜歡這個解決方案,但它有效,我沒有更好的解決方案。

1

AsyncTaskdoInBackground()拋出ClassCastException,因爲它試圖投LayerDrawableBitmapDrawable。提供可以安全地轉換爲BitmapDrawable的Drawable實例。