将列表保存到文件 android
*初始化变量
String serfilename;
List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>();
ON CREATE 方法
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 指针异常。我该如何解决这个问题...有更好的方法吗...?
*INITIALIZE VARIABLES
String serfilename;
List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>();
ON CREATE METHOD
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);
}
LOAD LIST FROM FILE METHOD
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;
}
WRITE LIST TO FILE METHOD
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() METHOD
protected void onStop(){
super.onStop();
writeListToFile((ArrayList<HashMap<String, String>>) painItems);
}
I am getting a NULL Pointer exception at File myfile = getFileStreamPath(serfilename);. How do I fix this.. is there a better way to do this...?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您粘贴的内容,您的
serfilename
为null
并且从未设置为路径。也就是说,即使没有这些,
由于您的(空)输入,预计也会抛出 NullPointerException 。
说服自己。尝试:
Based on what you have pasted, your
serfilename
isnull
and is never set to a path.That said, even without all that,
would be expected to throw a
NullPointerException
due to your (null) input.Convince yourself. Try: