将列表保存到文件 android

发布于 2024-11-25 02:38:16 字数 1905 浏览 1 评论 0原文

*初始化变量

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷︶言冷语的世界 2024-12-02 02:38:16

根据您粘贴的内容,您的 serfilenamenull 并且从未设置为路径。

也就是说,即使没有这些,

File myfile = getFileStreamPath(serfilename);

由于您的(空)输入,预计也会抛出 NullPointerException 。

说服自己。尝试:

if(null == serfilename)
    throw new RuntimeException ("serfilename is null!");

File myfile = getFileStreamPath(serfilename);

Based on what you have pasted, your serfilename is null and is never set to a path.

That said, even without all that,

File myfile = getFileStreamPath(serfilename);

would be expected to throw a NullPointerException due to your (null) input.

Convince yourself. Try:

if(null == serfilename)
    throw new RuntimeException ("serfilename is null!");

File myfile = getFileStreamPath(serfilename);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文