代号一:Android 构建中找不到资源

问题描述 投票:0回答:1

我正在使用 Codename One 构建一款小游戏。我的游戏可以在模拟器中运行,但是,每当我运行游戏的 Android 版本时,我都会遇到

java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.BufferedReader.close()' on a null object reference

我使用 if 语句来简单地关闭输入流(如果它不为空),但随后没有加载任何内容,在我看来,我没有正确配置我的资源。

这就是我加载图像资源的方式:

public static final String DUCKY_ATLAS = "/duckySprite.png";
public static final String LEVEL_ATLAS = "/mapSprite.png";

public static final String START_LEVEL = "/levelOne.png";
public static final String OBSTACLE_SEQUENCES = "/levelSequences.png";

//method to pull Image from file 
public static Image getSpriteAtlas(String file) {
    Image img = null;
    try {
        InputStream is = CN.getResourceAsStream(file);
        img = Image.createImage(is);
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return img;
}

我的资源文件夹的位置如下所示:/common/src/main/resources

其中也没有嵌套。

任何帮助将不胜感激。

java android codenameone inputstream
1个回答
0
投票
  1. 使用字符串文件名和路径创建一个文件,例如, 文件 file = new File("res/raw/filename.png");
  2. 尝试使用 fileinputstream 像 FileInputStream = new FileInputStream(file);

由于根本没有声明,可能会发生空指针异常。

© www.soinside.com 2019 - 2024. All rights reserved.