您好我想从资源文件夹为我的动态壁纸设置背景图片,在该图像上我想做一些其他的动画内容。那背景不应该改变。 这是我试过的代码,但没有用......
@Override
public void onCreate(SurfaceHolder holder) {
super.onCreate(holder);
Log.i("onCreate","onCreate");
Canvas c = null;
c = holder.lockCanvas();
try {
if(c != null)
doDraw(c);
} catch(Exception e) {
Log.e(TAG, "Exception Accourd ");
}
}
public void doDraw(Canvas c) {
Resources res = getResources();
Bitmap bm = BitmapFactory.decodeResource(res, R.drawable.bg_port);
c.drawBitmap(bm, 0, 0, null);
}
图片未加载,其捕获错误。
堆栈跟踪在下面给出
04-05 00:10:07.211: W/System.err(979): java.lang.NullPointerException
04-05 00:10:07.221: W/System.err(979): at com.exp.wallpaper.MyWallPaperService$MyWallPaperEngine.doDraw(MyWallPaperService.java:94)
04-05 00:10:07.221: W/System.err(979): at com.exp.wallpaper.MyWallPaperService$MyWallPaperEngine.onCreate(MyWallPaperService.java:125)
04-05 00:10:07.231: W/System.err(979): at android.service.wallpaper.WallpaperService$Engine.attach(WallpaperService.java:648)
04-05 00:10:07.231: W/System.err(979): at android.service.wallpaper.WallpaperService$IWallpaperEngineWrapper.executeMessage(WallpaperService.java:875)
04-05 00:10:07.241: W/System.err(979): at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:61)
04-05 00:10:07.241: W/System.err(979): at android.os.Handler.dispatchMessage(Handler.java:99)
04-05 00:10:07.241: W/System.err(979): at android.os.Looper.loop(Looper.java:123)
04-05 00:10:07.241: W/System.err(979): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-05 00:10:07.251: W/System.err(979): at java.lang.reflect.Method.invokeNative(Native Method)
04-05 00:10:07.251: W/System.err(979): at java.lang.reflect.Method.invoke(Method.java:507)
04-05 00:10:07.251: W/System.err(979): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-05 00:10:07.251: W/System.err(979): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-05 00:10:07.251: W/System.err(979): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
尝试此代码.......................
public void drawFrame() { final SurfaceHolder holder = getSurfaceHolder();
try {
c = holder.lockCanvas();
c.save();
if (c != null) {
doDraw(c);
}
} finally {
if (c != null) holder.unlockCanvasAndPost(c);
}
mHandler.removeCallbacks(onadraw);
if (mVisible) {
mHandler.postDelayed(onadraw, 1000/25);
}}
public void doDraw(Canvas c) {Resources res = getResources();
Bitmap bm = BitmapFactory.decodeResource(res, R.drawable.bg_port);
c.drawBitmap(bm, 0, 0, null);
}