基本上,我要做的是在一个活动中保存字符串的ArrayList,然后在另一个活动中读取它们。文件已创建(我可以在DDMS中看到它)但由于某种原因我无法获取活动来读取对象。
这是阅读代码:
try {
FileInputStream fis = new FileInputStream("purchased_songs.obj");
ObjectInputStream ois = new ObjectInputStream(fis);
purchasedSongs = (ArrayList<String>) ois.readObject();
ois.close();
for(int i=0;i<purchasedSongs.size();i++)
Log.d("purchased songs",purchasedSongs.get(i));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这是编写代码:
try {
FileOutputStream fos = openFileOutput("purchased_songs.obj",MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(purchasedSongs);
os.close();
}catch(Exception e){
e.printStackTrace();
}
答案 0 :(得分:0)
当然我弄清楚出了什么问题。
更改
FileInputStream fis = new FileInputStream("purchased_songs.obj");
到
FileInputStream fis = openFileInput("purchased_songs.obj");