*初始化变量
String serfilename;
List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>();
关于创建方法
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addscreen);
//getPainItems from the saved file
if(loadListFromFile((ArrayList<HashMap<String, String>>) painItems) !=
null)
painItems = loadListFromFile((ArrayList<HashMap<String, String>>)
painItems);
}
从文件方法加载列表
private ArrayList<HashMap<String, String>> loadListFromFile(
ArrayList<HashMap<String, String>> masterlistrev) {
try {
FileInputStream fis = openFileInput(serfilename);
ObjectInputStream ois = new ObjectInputStream(fis);
masterlistrev = (ArrayList<HashMap<String, String>>) ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return masterlistrev;
}
写列表到文件方法
private void writeListToFile(ArrayList<HashMap<String, String>> masterlistrev){
File myfile = getFileStreamPath(serfilename);
try {
if(myfile.exists() || myfile.createNewFile()){
FileOutputStream fos = openFileOutput(serfilename, MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(masterlistrev);
}
} catch (Exception e) {
e.printStackTrace();
}
}
ONSTOP() 方法
protected void onStop(){
super.onStop();
writeListToFile((ArrayList<HashMap<String, String>>) painItems);
}
我在 File myfile = getFileStreamPath(serfilename); 处得到一个 NULL 指针异常。我该如何解决这个问题......有没有更好的方法来做到这一点......?