2013-10-04 90 views
-4

我最近創建了自己的Android動態壁紙項目與18個圖像,但它不能設置爲壁紙。如果我將它設置爲牆紙,我會看到以下錯誤消息。不幸的是,示例停止工作。圖像分辨率480x800。我用了18張圖片。項目大小爲2.67 MB。但我在我的項目中找不到任何錯誤。任何人都知道這是什麼錯誤?Android動態壁紙不工作

public class star extends WallpaperService { 

    public void onCreate(){ 
     super.onCreate(); 
    } 

    public void onDestroy() { 
     super.onDestroy(); 
    } 

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

    private class WallpaperEngine extends Engine{ 

     Bitmap i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,i16,i17,i18,i19,i20,i21,i22,i23,i24,i25,i26,i27,i28,i29,i30; 
     int position; 
     int screenwidth; 
     DisplayMetrics metric; 
     int count=0; 

     private final Handler hanlder=new Handler(); 
     private boolean visible=false; 
     private final Runnable r=new Runnable() { 

      @Override 
      public void run() { 
       draw(); 
      } 
     }; 

     WallpaperEngine() { 
      i1=BitmapFactory.decodeResource(getResources(), R.drawable.image1); 
      i2=BitmapFactory.decodeResource(getResources(), R.drawable.image2); 
      i3=BitmapFactory.decodeResource(getResources(), R.drawable.image3); 
      i4=BitmapFactory.decodeResource(getResources(), R.drawable.image4); 
      i5=BitmapFactory.decodeResource(getResources(), R.drawable.image5); 
      i6=BitmapFactory.decodeResource(getResources(), R.drawable.image6); 
      i7=BitmapFactory.decodeResource(getResources(), R.drawable.image7); 
      i8=BitmapFactory.decodeResource(getResources(), R.drawable.image8); 
      i9=BitmapFactory.decodeResource(getResources(), R.drawable.image9); 
      i10=BitmapFactory.decodeResource(getResources(), R.drawable.image10); 
      i11=BitmapFactory.decodeResource(getResources(), R.drawable.image11); 
      i12=BitmapFactory.decodeResource(getResources(), R.drawable.image12); 
      i13=BitmapFactory.decodeResource(getResources(), R.drawable.image13); 
      i14=BitmapFactory.decodeResource(getResources(), R.drawable.image14); 
      i15=BitmapFactory.decodeResource(getResources(), R.drawable.image15); 
      i16=BitmapFactory.decodeResource(getResources(), R.drawable.image16); 
      i17=BitmapFactory.decodeResource(getResources(), R.drawable.image17); 
      i18=BitmapFactory.decodeResource(getResources(), R.drawable.image18); 
      i19=BitmapFactory.decodeResource(getResources(), R.drawable.image19); 
      i20=BitmapFactory.decodeResource(getResources(), R.drawable.image20); 
      position=0; 
      metric=getBaseContext().getResources().getDisplayMetrics(); 
      screenwidth= metric.widthPixels; 
     } 

     public void onCreate(SurfaceHolder holder){ 
      super.onCreate(holder); 
     } 

     public void onVisibilityChanged(boolean visible){ 
      this.visible=visible; 
      if (visible) { 
       hanlder.post(r); 
      } else { 
       hanlder.removeCallbacks(r); 
      } 
     } 

     public void onSurfaceDestroyed(SurfaceHolder holder){ 
      visible=false; 
      hanlder.removeCallbacks(r); 
     } 

     public void draw(){ 

      Bitmap arr[]={i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,i16,i17,i18,i19,i20,i19,i18,i17,i16,i15,i14,i13,i12,i11,i10,i9,i8,i7,i6,i5,i4,i3,i2}; 
      SurfaceHolder holder=getSurfaceHolder(); 
      Canvas c=null; 

      try { 

       c=holder.lockCanvas(); 
       if(c!=null){ 
        c.drawColor(Color.WHITE); 
        c.drawBitmap(arr[count], position,0, null); 
        ++count; 

        if(count==arr.length){ 
         count=0; 
        } 
       } 

      } finally { 
       if(c!=null){ 
        holder.unlockCanvasAndPost(c); 
       } 
      } 

      hanlder.removeCallbacks(r); 
      if(visible){ 
       hanlder.postDelayed(r, 70); 
      } 

     } 
    } 
} 

後,我收到以下錯誤logcat中:

09-26 14:07:01.005: E/AndroidRuntime(2767): FATAL EXCEPTION: main 
09-26 14:07:01.005: E/AndroidRuntime(2767): java.lang.OutOfMemoryError 
09-26 14:07:01.005: E/AndroidRuntime(2767):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 

09-26 14:07:01.005: E/AndroidRuntime(2767):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502) 

09-26 14:07:01.005: E/AndroidRuntime(2767):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355) 

09-26 14:07:01.005: E/AndroidRuntime(2767):  at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:378) 

09-26 14:07:01.005: E/AndroidRuntime(2767):  at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:408) 

09-26 14:07:01.005: E/AndroidRuntime(2767):  at com.example.stargate.star$WallpaperEngine.<init>(star.java:55) 

09-26 14:07:01.005: E/AndroidRuntime(2767):  at com.example.stargate.star.onCreateEngine(star.java:27) 

09-26 14:07:01.005: E/AndroidRuntime(2767):  at android.service.wallpaper.WallpaperService$IWallpaperEngineWrapper.executeMessage(WallpaperService.java:1034) 

09-26 14:07:01.005: E/AndroidRuntime(2767):  at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:40) 

09-26 14:07:01.005: E/AndroidRuntime(2767):  at android.os.Handler.dispatchMessage(Handler.java:99) 

09-26 14:07:01.005: E/AndroidRuntime(2767):  at android.os.Looper.loop(Looper.java:137) 

09-26 14:07:01.005: E/AndroidRuntime(2767):  at android.app.ActivityThread.main(ActivityThread.java:5041) 

09-26 14:07:01.005: E/AndroidRuntime(2767):  at java.lang.reflect.Method.invokeNative(Native Method) 

09-26 14:07:01.005: E/AndroidRuntime(2767):  at java.lang.reflect.Method.invoke(Method.java:511) 

09-26 14:07:01.005: E/AndroidRuntime(2767):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 

09-26 14:07:01.005: E/AndroidRuntime(2767):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 

09-26 14:07:01.005: E/AndroidRuntime(2767):  at dalvik.system.NativeStart.main(Native Method) 

09-26 14:08:11.935: E/Trace(4999): error opening trace file: No such file or directory (2) 
+3

如果您收到錯誤消息,您的代碼可能不是100%正確的。 – nhgrif

+3

如果您收到錯誤消息,請將其發佈。請給我Logcat。 – Geobits

+1

某處存在未捕獲的異常。用附加的設備運行壁紙,並在logcat中查找錯誤。 *編輯*寧可尋找由錯誤導致的大型長堆棧跟蹤。 –

回答

1

你爲什麼要借鑑彼此的排名前18位圖?你的內存不足了!只有在你需要時才解碼並加載它們!

將代碼縮減爲3位圖並查看錯誤是否仍然存在,我敢打賭它不會。

+0

謝謝,我會嘗試它的朋友 – droidliveman

0

您可以使用此代碼。

class Diwali extends Engine { 

    private boolean mVisible; 

    private final Runnable diwaliImg = new Runnable() { 
     public void run() { 
      drawFrame(); 
     } 
    }; 

    int i=0; 
    int[] pirates = { 
     R.drawable.a1, R.drawable.a2, 
     R.drawable.a3, R.drawable.a4, 
     R.drawable.a5, R.drawable.a6, 
     R.drawable.a7, R.drawable.a8, 
     R.drawable.a9, R.drawable.a10, 
     R.drawable.a11, R.drawable.a12, 
     R.drawable.a13, R.drawable.a14 
    }; 

    @Override 
    public void onCreate(SurfaceHolder holder){ 
     super.onCreate(holder); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     mHandler.removeCallbacks(diwaliImg); 
    } 

    @Override 
    public void onVisibilityChanged(boolean visible) { 
     mVisible = visible; 
     if (visible) { 
      drawFrame(); 
     } else { 
      mHandler.removeCallbacks(diwaliImg); 
     } 
    } 

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

    @Override 
    public void onSurfaceCreated(SurfaceHolder holder) { 
     // TODO Auto-generated method stub 
     super.onSurfaceCreated(holder); 
    } 

    @Override 
    public void onSurfaceDestroyed(SurfaceHolder holder) { 
     // TODO Auto-generated method stub 
     super.onSurfaceDestroyed(holder); 
     mVisible = false; 
     mHandler.removeCallbacks(diwaliImg); 
    } 


    @Override 
    public void onOffsetsChanged(float xOffset, float yOffset, float xStep,float yStep, int xPixels, int yPixels) { 
     drawFrame(); 
    } 

    @Override 
    public void onTouchEvent(MotionEvent event) { 
     super.onTouchEvent(event); 
    } 

    private void drawFrame() { 
     // TODO Auto-generated method stub 
     final SurfaceHolder holder = getSurfaceHolder(); 
     Canvas c = null; 
     try { 
      c = holder.lockCanvas(); 
      if (c != null) { 
       drawPirate(c); 
      } 
     } finally { 
      if (c != null) 
       holder.unlockCanvasAndPost(c); 
     } 
     mHandler.removeCallbacks(diwaliImg); 
     if (mVisible) { 
      mHandler.postDelayed(diwaliImg,60); 
     } 
    } 

    private void drawPirate(Canvas c) { 
     // TODO Auto-generated method stub 
     Bitmap icon = BitmapFactory.decodeResource(getResources(),pirates[i]); 

     i++; 

     if (i == 13) { 
      i = 0; 
     } 
     Matrix matrix = new Matrix(); 

     c.drawBitmap(icon, matrix, null); 
     icon.recycle(); 
    } 
}