Android app(java): 无法读取文件且未出现错误消息
Android app(java): can't read a file and no error message appears
我的问题可能不完全是我无法读取文件,而是应该接收该文件信息的 class 没有该信息。我正在尝试使用瓦片地图制作 libGdx 的游戏。我想创建一个使用 4 ArrayList<ArrayList<Integer>>
(每个象限一个)的自定义瓦片地图,以便为我的地图创建一个基础结构。这些仅用于游戏知道要绘制和绘制的位置(我只绘制屏幕上的内容)。地图工作正常:我可以使用键盘输入对其进行编辑、保存和加载。在桌面上它工作正常但在我的 android phone 上地图是空的(我使用 System.out.println(map.tilesQ1);
并得到 []
)。我使用以下方法获取纹理:
new Texture("textures/" + int + ".png");
并且也适用于 android,但我不知道为什么它不能得到 "maps/map1"。
我保存了带文件扩展名和不带文件扩展名的文件,并尝试了不同的前缀组合,但似乎没有任何效果。我的 smartphone 是 Moto G 3,具有 Lineage OS,所以我可以访问 root。我使用根资源管理器,我知道我要查找的文件在“...apk/assets/maps/”中。
public TileMapHandler(){
}
public static boolean saveMap(String fileName, TileMap map){
FileOutputStream fos = null;
ObjectOutputStream out = null;
Object[] mapArrays = {map.tilesQ1, map.tilesQ2, map.tilesQ3, map.tilesQ4};
try{
fos = new FileOutputStream(fileName);
out = new ObjectOutputStream(fos);
out.writeObject(mapArrays);
out.close();
fos.close();
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
saveFile("/maps/lastSave", "fileName");
return true;
}
public static void loadMap(String fileName, TileMap map){
FileInputStream fis = null;
ObjectInputStream in = null;
try{
if(new File(fileName).exists()) {
fis = new FileInputStream(fileName);
in = new ObjectInputStream(fis);
Object[] mapArrays = (Object[]) in.readObject();
if(mapArrays[0] instanceof ArrayList)
map.tilesQ1 = (ArrayList<ArrayList<Integer>>) mapArrays[0];
map.tilesQ2 = (ArrayList<ArrayList<Integer>>) mapArrays[1];
map.tilesQ3 = (ArrayList<ArrayList<Integer>>) mapArrays[2];
map.tilesQ4 = (ArrayList<ArrayList<Integer>>) mapArrays[3];
saveFile("./maps/lastSave", fileName);
fis.close();
in.close();
}
else{
return;
}
} catch(Exception ex){
ex.printStackTrace();
System.out.println("Load map error");
return;
}
saveFile("./maps/lastLoad", fileName);
if(new File("./maps/recent").exists()){
ArrayList<String> list = (ArrayList<String>) loadFile("./maps/recent");
if(!list.contains(fileName)) {
list.add(fileName);
saveFile("./maps/recent", list);
}
}
else{
ArrayList list = new ArrayList<String>();
list.add(fileName);
saveFile("./maps/recent", list);
}
}
这只是我 "level class":
的开始
public TestLevel(BaseGame g) {
super(g);
}
@Override
public void create() {
map = new TileMap();
TileMapHandler.loadMap("maps/map1", map);
System.out.println(map.tilesQ1);
正如我所说:它可以在桌面上运行,但不能在我的 android 设备上运行,而且我什至没有遇到异常。
我没能听懂你们给我的建议。我在两个平台上工作,我不能在核心包中使用 android 特定方法或 类。我发现这个问题的解决方案是让核心启动器接收一个接口作为参数,并让每个平台特定的启动器创建一个实例,如果该接口具有平台特定的代码,这样应用程序就可以在 android 上运行。不幸的是,我无法让它发挥作用。每次我尝试从接口实例调用方法时,我都会收到一个用于调用接口方法的 NullPointerException(有什么建议吗?教程:https://github.com/libgdx/libgdx/wiki/Interfacing-with-platform-specific-code). I tried using libGdx methods (I should have searched about it before) and it worked. https://github.com/libgdx/libgdx/wiki/File-handling
谢谢大家。
我的问题可能不完全是我无法读取文件,而是应该接收该文件信息的 class 没有该信息。我正在尝试使用瓦片地图制作 libGdx 的游戏。我想创建一个使用 4 ArrayList<ArrayList<Integer>>
(每个象限一个)的自定义瓦片地图,以便为我的地图创建一个基础结构。这些仅用于游戏知道要绘制和绘制的位置(我只绘制屏幕上的内容)。地图工作正常:我可以使用键盘输入对其进行编辑、保存和加载。在桌面上它工作正常但在我的 android phone 上地图是空的(我使用 System.out.println(map.tilesQ1);
并得到 []
)。我使用以下方法获取纹理:
new Texture("textures/" + int + ".png");
并且也适用于 android,但我不知道为什么它不能得到 "maps/map1"。 我保存了带文件扩展名和不带文件扩展名的文件,并尝试了不同的前缀组合,但似乎没有任何效果。我的 smartphone 是 Moto G 3,具有 Lineage OS,所以我可以访问 root。我使用根资源管理器,我知道我要查找的文件在“...apk/assets/maps/”中。
public TileMapHandler(){
}
public static boolean saveMap(String fileName, TileMap map){
FileOutputStream fos = null;
ObjectOutputStream out = null;
Object[] mapArrays = {map.tilesQ1, map.tilesQ2, map.tilesQ3, map.tilesQ4};
try{
fos = new FileOutputStream(fileName);
out = new ObjectOutputStream(fos);
out.writeObject(mapArrays);
out.close();
fos.close();
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
saveFile("/maps/lastSave", "fileName");
return true;
}
public static void loadMap(String fileName, TileMap map){
FileInputStream fis = null;
ObjectInputStream in = null;
try{
if(new File(fileName).exists()) {
fis = new FileInputStream(fileName);
in = new ObjectInputStream(fis);
Object[] mapArrays = (Object[]) in.readObject();
if(mapArrays[0] instanceof ArrayList)
map.tilesQ1 = (ArrayList<ArrayList<Integer>>) mapArrays[0];
map.tilesQ2 = (ArrayList<ArrayList<Integer>>) mapArrays[1];
map.tilesQ3 = (ArrayList<ArrayList<Integer>>) mapArrays[2];
map.tilesQ4 = (ArrayList<ArrayList<Integer>>) mapArrays[3];
saveFile("./maps/lastSave", fileName);
fis.close();
in.close();
}
else{
return;
}
} catch(Exception ex){
ex.printStackTrace();
System.out.println("Load map error");
return;
}
saveFile("./maps/lastLoad", fileName);
if(new File("./maps/recent").exists()){
ArrayList<String> list = (ArrayList<String>) loadFile("./maps/recent");
if(!list.contains(fileName)) {
list.add(fileName);
saveFile("./maps/recent", list);
}
}
else{
ArrayList list = new ArrayList<String>();
list.add(fileName);
saveFile("./maps/recent", list);
}
}
这只是我 "level class":
的开始public TestLevel(BaseGame g) {
super(g);
}
@Override
public void create() {
map = new TileMap();
TileMapHandler.loadMap("maps/map1", map);
System.out.println(map.tilesQ1);
正如我所说:它可以在桌面上运行,但不能在我的 android 设备上运行,而且我什至没有遇到异常。
我没能听懂你们给我的建议。我在两个平台上工作,我不能在核心包中使用 android 特定方法或 类。我发现这个问题的解决方案是让核心启动器接收一个接口作为参数,并让每个平台特定的启动器创建一个实例,如果该接口具有平台特定的代码,这样应用程序就可以在 android 上运行。不幸的是,我无法让它发挥作用。每次我尝试从接口实例调用方法时,我都会收到一个用于调用接口方法的 NullPointerException(有什么建议吗?教程:https://github.com/libgdx/libgdx/wiki/Interfacing-with-platform-specific-code). I tried using libGdx methods (I should have searched about it before) and it worked. https://github.com/libgdx/libgdx/wiki/File-handling 谢谢大家。