我目前正在为我们高中的小组制作一个应用程序。我们决定制作一个个人理财应用程序来组织您的支出。我正在尝试使用内部存储来存储数据。我为此创建了一个InternalStorage
类(这是其他人对像我这样的问题的回答,但我忘记了从哪里来的。哎呀。),具有相应write
的read
方法。但是,在调试时,我发现了一些令人难以置信的行为。
public final class InternalStorage {
private static String key = "billify";
public static void write(Context context, List<Bill> billList){
try{
FileOutputStream fos = context.openFileOutput(key, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(billList);
oos.close();
fos.close();
}catch(IOException e){
Toast.makeText(context,"Internal storage write error: IOException", Toast.LENGTH_LONG).show();
}
}
public static List<Bill> read(Context context){
try {
FileInputStream fis = context.openFileInput(key);
ObjectInputStream ois = new ObjectInputStream(fis);
List<Bill> a = (List<Bill>) ois.readObject();//jumps from here
return a;
}catch(Exception e){
e.printStackTrace();
Toast.makeText(context,"Internal storage read error",Toast.LENGTH_LONG).show();
return null;//jumps to here w/o triggering previous two lines.
}
}
}
我在所有代码行中都设置了断点,read()
从我所见,代码运行ois.readObject()
,然后没有运行 e.printStackTrace() 和 Toast,直接跳回到return null
.
有人知道发生了什么吗?
编辑:
if(z.getClass() == cls){
ArrayList<Bill> a = (ArrayList<Bill>) z;//Jumps from here now
return a;
}
return null;
进行检查转换仍然会发生同样的事情-类是相同的,但是进行转换仍然会使其跳转到上一个,最后一个null
。