我最近創建了自己的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)
如果您收到錯誤消息,您的代碼可能不是100%正確的。 – nhgrif
如果您收到錯誤消息,請將其發佈。請給我Logcat。 – Geobits
某處存在未捕獲的異常。用附加的設備運行壁紙,並在logcat中查找錯誤。 *編輯*寧可尋找由錯誤導致的大型長堆棧跟蹤。 –