Android文件IO:不保存/不加载数据

时间:2016-08-10 17:48:06

标签: java android android-fragments android-activity file-io

背景:

我在我的应用程序中使用了三个片段和一个活动。两个片段使用recyclerViews,另一个使用expandableListView。

问题:

我正在尝试正确编程onPause(),onResume(),onStop()和onRestart()方法,以便在按下home,back或switch视图按钮时保存应用程序的状态。

要保存程序的状态并在程序返回时加载它,我已经在我唯一的活动中创建了save()和load()方法。

//from the end of onCreate
    load();
}

@Override
protected void onStart() {
    super.onStart();
    mRef = new Firebase("https://sure-7856d.firebaseio.com/");
}
@Override
protected void onStop(){
    super.onStop();
    save();
}
@Override
protected void onPause(){
    super.onPause();
    save();
}
@Override
protected void onResume(){
    super.onResume();
    load();
}
@Override
protected void onRestart(){
    super.onRestart();
    load();
}

在save方法中,我从我的2个recyclerViews和我的expandableListView的ArrayList中获取适配器,它跟踪检查了哪些复选框。 之后我将它们分别放入一个临时的Arraylist中,然后将每个ArrayLists添加到一个将保存到文件中的arraylist中。

private void save(){

    //saved_data = new File("saved_data");


    /*try {
        if(saved_data.exists()==false){
            //saved_data.createNewFile();
            saved_data.setWritable(true);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }*/

    File myFile;
    try{
        myFile = new File(getFilesDir().getAbsolutePath(), "dir/save_data.bin");
        myFile.mkdirs();
        myFile.createNewFile();
        FILENAME = myFile.getName();
    }catch (IOException e){
        e.printStackTrace();
    }

    OneFragment f20 = (OneFragment) frags.get(0);
    TwoFragment f21 = (TwoFragment) frags.get(1);
    ThreeFragment f22 = (ThreeFragment) frags.get(2);

    ArrayList saveTasks = f20.adapter.getList();
    ArrayList saveReqs = f21.adapter.getList();
    ArrayList saveMap = new ArrayList<String>();
    if(f22.listAdapter!=null) {
        if (f22.listAdapter.getExport() != null) {
            saveMap = f22.listAdapter.getExport();
        }
    }


    ArrayList results = new ArrayList();
    results.add(saveTasks);
    results.add(saveReqs);
    results.add(saveMap);

    ObjectOutputStream oos1 = null;
    FileOutputStream fos1 = null;

    try {
        fos1 = openFileOutput(FILENAME, Context.MODE_PRIVATE);
        oos1 = new ObjectOutputStream(fos1);
        oos1.writeObject(results);
        oos1.close();
        fos1.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if(oos1  != null){
            try {
                oos1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

在加载代码中,我从文件中获取对象并将适配器更新为先前的状态。

private void load(){

    //saved_data = new File("saved_data");

    ArrayList results = new ArrayList();
    FileInputStream fis = null;
    ObjectInputStream ois = null;
    if(FILENAME==null){
        return;
    }
    try {
        fis = new FileInputStream(FILENAME);
        ois = new ObjectInputStream(fis);
    }catch (FileNotFoundException e) {
        e.printStackTrace();
        return;
    }catch (IOException e) {
        e.printStackTrace();
    }

    try {
        while (true) {
            results = (ArrayList)(ois.readObject());
        }
    } catch (OptionalDataException e) {
        if (!e.eof) try {
            throw e;
        } catch (OptionalDataException e1) {
            e1.printStackTrace();
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            ois.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    for(int i = 0; i < results.size();i++){
            if(i == 0){
                frags.set(0,results.get(i));
            }
            else if(i == 1){
                frags.set(1,results.get(i));
            }
            else if(i == 2){
                frags.set(2,results.get(i));
            }
            else{

            }
    }

}

当我按下模拟器底部的方形按钮并返回时,会发生以下几种情况之一

崩溃说文件无法找到

 W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)

此错误指向负载方法作为故障点

FileInputStream fis = null;

当我点击三角形按钮时,logcat中没有任何反应,一切都在返回时消失,应用程序失去了将函数添加到recyclerViews上的列表并在expandableListView中显示两个列表的功能。

点击中心圆圈按钮然后返回应用程序就可以了。没有任何内容。

因为我m getting a file not found error I think that the file isn写了

我已经搜索了Stack的解决方案,我是File IO和片段的新手,所以我不知道从哪里开始。

0 个答案:

没有答案