尝试加载资源时的ResourcesNotFoundException

时间:2016-07-04 12:05:41

标签: android intellij-idea android-resources

我得到了

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

当我尝试从res / drawable文件夹中的资源加载位图时(甚至是IntelliJ auto-complete找到它们),如下所示:

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);
    }

我只传了两次资源:

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

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

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

}

在这里:

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

在这里,您可以看到我的directory

1 个答案:

答案 0 :(得分:0)

Resources.getSystem()是您的问题所在。它返回一个全局共享的Resources对象,但不返回该应用程序的对象。您应该使用Context.getResource()代替

  

应用程序包的资源实例

在这里,您可以找到Context.getResource()Resources.getSystem()

的文档