问题
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)
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 }
So the exception occurs when bm.eraseColor(0) is called?
Can somebody help me? Thanks!
回答1:
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.
Hope this will help you.
回答2:
I traced several intermittent errors where Bitmap.createBitmap() returned null; they happened mostly in Chinese Android phones. The parameters being passed were ok, but I found that the jvm was very low on memory whenever this occurs.
My theory is there are cases where createBitmap() runs out of memory and returns null instead of throwing OutOfMemoryException (which is what it usually does).
My "fix" is to check if createBitmap() returns null and throw our own OutOfMemoryException so it is correctly categorized. Then our error logging treats it as a device issue rather than a functional bug.
来源:https://stackoverflow.com/questions/8902852/android-createbitmap-nullpointerexception