英文:
Codename One: Resources Not Being Found in Android Build
问题
我正在使用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";
// 从文件中获取图像的方法
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
它内部也没有嵌套。
非常感谢您的任何帮助。
英文:
I am using Codename One to build a little game. My game works in the simulator, however, whenever I run an Android build of the game, I'm met with
java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.BufferedReader.close()' on a null object reference
I used an if statement to simply close an input stream if it is not null but then nothing loads in, which seems to me I am not configuring my resources correctly.
This is how I am loading in my image resources:
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;
}
The location of my resources folder looks like : /common/src/main/resources
There is also no nesting within it.
Any help would be greatly appreciated.
答案1
得分: 1
- 使用带有路径的字符串文件名创建一个文件,例如:
File file = new File("res/raw/filename.png");
- 尝试使用
FileInputStream
,如下所示:
FileInputStream is = new FileInputStream(file);
请注意,可能会发生 NullPointerException
,因为根本没有声明。
英文:
- Create a file using your string filename with path, for example:
File file = new File("res/raw/filename.png");
- Try to use
FileInputStream
like so:
FileInputStream is = new FileInputStream(file);
Note that NullPointerException
may happen because of there is no declaration at all.
答案2
得分: 1
检查文件夹中的文件是否与您使用的大小写匹配,因为文件名对大小写敏感。我建议将找不到的特定文件名显示为错误,这样您就会知道哪个文件丢失了。
除此之外,您应该使用Java 8的新语法之一,try-with-resources:
try (InputStream is = CN.getResourceAsStream(file)) {
img = Image.createImage(is);
} catch (IOException e) {
Log.e(e);
Dialog.show("Error", "Loading file failed: " + file, "OK", null);
}
return img;
这样可以省去关闭的需要。此外,始终使用 Log.e()
而不是 printStackTrace
。
英文:
Check that the files in the folder match the case you use as even the file names are case sensitive. I suggest showing the specific file name that wasn't found as an error so you'll know which file is missing.
Other than that you should use the newer syntax of try-with-resources as part of Java 8:
try(InputStream is = CN.getResourceAsStream(file)) {
img = Image.createImage(is);
} catch (IOException e) {
Log.e(e);
Dialog.show("Error", "Loading file failed: " + file, "OK", null);
}
return img;
This removes the need to close. Also, always use Log.e()
instead of printStackTrace
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论