0

嗨,我想从资源文件夹中为我的动态壁纸设置背景图像,在该图像上我想做一些其他动画的东西。这种背景不应该改变。这是我尝试过的代码,但没有工作......

@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)
4

1 回答 1

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);
    }
于 2013-04-25T19:51:00.313 回答