安卓createBitmap NullPointerException异常 [英] android createBitmap NullPointerException

查看:262
本文介绍了安卓createBitmap NullPointerException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示java.lang.NullPointerException当我尝试打电话给createBitmap。这里是我的code:

I got a java.lang.NullPointerException when I try to call createBitmap. Here is my code:

board = Bitmap.createBitmap(handler.signs.length * cellSize, 
        handler.signs[0].length * cellSize, Bitmap.Config.ARGB_8888);

,其中宽度和高度也大于零。这就是堆栈跟踪:

where the width and height are also greater than zero. And this is the stack trace:

java.lang.NullPointerException
at android.graphics.Bitmap.createBitmap(Bitmap.java:478)
at org.me.five_stones_project.game.GameView.drawBoard(GameView.java:334)
at org.me.five_stones_project.game.GameView.increaseBoard(GameView.java:293)
at org.me.five_stones_project.game.GameHandler.checkStatus(GameHandler.java:304)
at org.me.five_stones_project.game.GameHandler.makeMyStep(GameHandler.java:211)
at org.me.five_stones_project.game.GameView.onTouchEvent(GameView.java:238)
at android.view.View.dispatchTouchEvent(View.java:3891)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1719)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1150)
at android.app.Activity.dispatchTouchEvent(Activity.java:2102)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1703)
at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2200)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1884)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:861)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:619)
at dalvik.system.NativeStart.main(Native Method)

我接过一看位图源$ C ​​$ C,我发现这一点:

I took a look at the source code of the Bitmap and I found this:

476         public static Bitmap createBitmap(int width, int height, Config config) {
477         Bitmap bm = nativeCreate(null, 0, width, width, height, config.nativeInt, true);
478         bm.eraseColor(0);    // start with black/transparent pixels
479         return bm;
480     }

所以,当 bm.eraseColor(0)的被称为出现异常?

有人可以帮我吗?谢谢!

Can somebody help me? Thanks!

推荐答案

尝试使用 createScaledBitmap 相反,我之前面临同样的问题,我记得的原因是
要为位图的宽度或高度超过该装置的尺寸。

Try use createScaledBitmap instead, I face the same problem before, the reason i remember is the width or the height you want for the bitmap exceeds the size of the device.

希望这会帮助你。

这篇关于安卓createBitmap NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆