0

I get

Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f060000

when I try to load a bitmap from a resource in my res/drawable folder (even intelliJ auto-complete finds them) like so:

public Sprite(int resource, int numberOfFrames){
    BitmapRegionDecoder spriteSheet = null;
    try {
        InputStream is = Resources.getSystem().openRawResource(+resource);
        spriteSheet = BitmapRegionDecoder.newInstance(is, false);
    } catch (IOException ioexc){
        ioexc.printStackTrace();
    }
    timer = 0;
    this.numberOfFrames = numberOfFrames;
    frames = new Bitmap[numberOfFrames];
    for (int i = 0; i < numberOfFrames; i++) {
        frames[i] = spriteSheet.decodeRegion(new Rect(i*32, 0, (i*32)+32, 32), null);
    }

and I pass the resource only 2 times:

public class PlayerSprite extends Sprite {
    private boolean isHitting;
    private int timer;

    public PlayerSprite() {
        super(R.drawable.player_sprite_sheet, 3);
        timer = 0;
    }

 //...more core...//

}

and here:

sprite = new Sprite(R.drawable.ball_sprite, 7);

Here you can see my directory

1 Answers1

0

Resources.getSystem() is the problem in your case. It returns a global shared Resources object but not the one of the application. You should use Context.getResource() instead, which returns

a resources instance for the application's package

here you can find the documentation for Context.getResource() and for Resources.getSystem()

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • hmm, I get an error saying Context doesn't have getResource() method, do I need to pass a context by a constructor or something? – Криси Стоянов Jul 04 '16 at 12:23
  • yes you have to. If you are subclassing view, you can use `getContext()` – Blackbelt Jul 04 '16 at 12:34
  • ohh this is bad, I will have to pass it through several constructors at best, do I need to get the context from my view class, since my view class isn't connected to my sprite classes. Also I am converting my java swing app to android, so i am not very familiar with android. – Криси Стоянов Jul 04 '16 at 15:23
  • probably the Application's context for this use case is enough. Have a look [here](http://stackoverflow.com/questions/14057273/android-singleton-with-global-context) – Blackbelt Jul 04 '16 at 15:26