1

In my current android project I create two bitmaps from xml resources. Every time I build my project using the xml bitmaps as the resource for Bitmap.decodeResources() I get a null pointer exception when I try to access the bitmaps. But when I switch to using the straight .png the error goes away.

Why is decodeResources() returning null?

code where the bitmaps are created:

public OvertButton(float x, float y, float width, float height, int pressedId, int  unPressedId, Resources res, boolean visible) {
    \\these are null if xml bitmap is passed
    pressed = BitmapFactory.decodeResource(res, pressedId); 
    unPressed = BitmapFactory.decodeResource(res, unPressedId);

    this.visible = visible;
    relocate(x, y);
    resize(width, height); //throws NPE when I createScaledBitmap()

    ClickableManager.registerClickable(this);
}

resource file

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/play_button_pressed"
    android:filter="false"
    android:antialias="false"
    android:dither="false"/>

and the resize method

@Override
    public void resize(float width, float height) {
        box.set(box.left, box.top, box.left + width, box.top + height);
        Log.d("resize",String.valueOf(height));
        pressed = Bitmap.createScaledBitmap(pressed, (int)width, (int)height, true);
        unPressed = Bitmap.createScaledBitmap(unPressed, (int)width, (int)height, true);
    }

And the logcat

04-16 09:45:06.341  11824-11824/com.handmade.eed D/dalvikvm﹕ Late-enabling CheckJNI
04-16 09:45:06.450  11824-11824/com.handmade.eed D/MenuActivity﹕ 2130837594$$$2130837596
04-16 09:45:06.451  11824-11824/com.handmade.eed D/skia﹕ --- SkImageDecoder::Factory returned null
04-16 09:45:06.451  11824-11824/com.handmade.eed D/OvertButton﹕ true2130837591
04-16 09:45:06.457  11824-11824/com.handmade.eed D/OvertButton﹕ false2130837595
04-16 09:45:06.457  11824-11824/com.handmade.eed D/resize﹕ 252.0
04-16 09:45:06.458  11824-11824/com.handmade.eed D/AndroidRuntime﹕ Shutting down VM
04-16 09:45:06.459  11824-11824/com.handmade.eed W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41610d40)
04-16 09:45:06.461  11824-11824/com.handmade.eed E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.handmade.eed, PID: 11824
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.handmade.eed/com.handmade.eed.MenuActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5102)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:615)
            at com.handmade.overt.visible.OvertButton.resize(OvertButton.java:61)
            at com.handmade.overt.visible.OvertButton.<init>(OvertButton.java:29)
            at com.handmade.overt.visible.OvertButton.<init>(OvertButton.java:34)
            at com.handmade.eed.MenuActivity.onCreate(MenuActivity.java:72)
            at android.app.Activity.performCreate(Activity.java:5248)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5102)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
04-16 09:45:09.872  11824-11824/com.handmade.eed I/Process﹕ Sending signal. PID: 11824 SIG: 9
4
  • post resize() method.. Commented Apr 16, 2014 at 17:23
  • posted. Its just createScaledBitmap Commented Apr 16, 2014 at 17:27
  • have you passded the correct ids?? Commented Apr 16, 2014 at 17:30
  • @kalyanpvs Yes, that's why I'm confused. If I pass R.drawable.play_button_pressed_res (the xml above) it throws a NPE but if I pass R.drawable.play_button_pressed (the png) the program will run fine. Commented Apr 16, 2014 at 17:34

1 Answer 1

1

This is a case of me being dumb but here is the answer for posterity. Bitmap is not the same as BitmapDrawable

From the Drawable Resources link above:

COMPILED RESOURCE DATATYPE:
Resource pointer to a BitmapDrawable.

Thus I had to change this:

Bitmap pressed = BitmapFactory.decodeResource(res, R.drawable.play_button_pressed_res); //this is the id for the xml referring to the png

to either this:

Drawable pressed = res.getDrawable(R.drawable.play_button_pressed_res);

or this:

Bitmap pressed = BitmapFactory.decodeResource(res, R.drawable.play_button_pressed); //this is the id for the png 

I decided to use bitmap because, as I understand, it is faster.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.