Java Android:将arrayList保存到文件无效

Java Android:将arrayList保存到文件无效,java,android,file,arraylist,save,Java,Android,File,Arraylist,Save,我有一个活动DrinkWater,它包含recyclerView。我正试图将名为数据的arrayList保存到内部存储器中,以备将来参考,但不知何故,它不起作用 我确实在清单文件中添加了权限,所以这不是问题所在 下面是DrinkWater.java文件的(简化)代码。为了调试,我做了几次祝酒。因此,不要错过//注释 public class DrinkWater extends ActionBarActivity { Toolbar toolbar; private Recycle

我有一个活动
DrinkWater
,它包含
recyclerView
。我正试图将名为
数据的
arrayList
保存到内部存储器中,以备将来参考,但不知何故,它不起作用

我确实在清单文件中添加了权限,所以这不是问题所在

下面是
DrinkWater.java
文件的(简化)代码。为了调试,我做了几次祝酒。因此,不要错过//注释

    public class DrinkWater extends ActionBarActivity {

Toolbar toolbar;
private RecyclerView recyclerView;
private InfoTodayAdapter adapter;
FileOutputStream fos;
FileInputStream fis;
List<InformationToday> data = new ArrayList<>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drink_water);

    toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);

    recyclerView = (RecyclerView) findViewById(R.id.recyclerview_today);
    adapter = new InfoTodayAdapter(this,getData());
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
}

public List<InformationToday> getData(){
    try {
        fis = getBaseContext().openFileInput("arrayListToday");
        ObjectInputStream ois = new ObjectInputStream(fis);
        //Below Toast just for debugging. 
        Toast.makeText(this,"Try block for read data called 1",Toast.LENGTH_SHORT).show(); 
        data = (List<InformationToday>) ois.readObject(); // Cause of the error. 
        //Below toast for debugging. But it is never executed.
        Toast.makeText(this,"Try block for read data called 2",Toast.LENGTH_SHORT).show();  
        ois.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Log.d("TAG A","arrayListToday file not found");
    } catch (Exception e){
        Log.d("TAG A","Exception generated 1");
    }
    return data;
}


public void addWater(View view) {   // This method adds data to the recyclerView. It is called by on click of the Floating Action Button
    InformationToday i = new InformationToday();
    if(view.getId() == R.id.fifty_button)
    {
        i.volume = "50";
        Log.d("TAG A","i.volume = 50");
    }
    else
    {
        i.volume = "100";
    }
    data.add(0,i);          
    adapter.update(data);   //This method is described in recyclerView adapter. It updates the recyclerView data.
    try {
        fos = getBaseContext().openFileOutput("arrayListToday",Context.MODE_PRIVATE);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        Toast.makeText(this,"Try block for wrtie data called 1",Toast.LENGTH_SHORT).show(); //For debugging.
        oos.writeObject(data); // Cause of the error. 
        Toast.makeText(this,"Try block for write data called 2",Toast.LENGTH_SHORT).show(); //For debugging. It is never executed.
        oos.close();
    } catch (FileNotFoundException e) {
        Toast.makeText(this,"Catch: File not gound exception",Toast.LENGTH_SHORT).show();
        Log.d("TAG A", "Catch: File not found");
        e.printStackTrace();
    }catch (Exception e){
        Toast.makeText(this,"Catch: Exception e",Toast.LENGTH_SHORT).show();
        Log.d("TAG A","Catch: Exception e");
    }
    com.getbase.floatingactionbutton.FloatingActionsMenu f = (FloatingActionsMenu) findViewById(R.id.multiple_actions);
    f.collapseImmediately();

}
}
公共类DrinkWater扩展了ActionBarActivity{
工具栏;
私人回收站;
私有InfoToDayaAdapter适配器;
文件输出流;
文件输入流fis;
列表数据=新的ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、活动、饮用水);
工具栏=(工具栏)findViewById(R.id.app\u栏);
设置支持操作栏(工具栏);
recyclerView=(recyclerView)findViewById(R.id.recyclerView_today);
adapter=newinfotodayadapter(this,getData());
recyclerView.setAdapter(适配器);
recyclerView.setLayoutManager(新的LinearLayoutManager(本));
}
公共列表getData(){
试一试{
fis=getBaseContext().openFileInput(“arrayListToday”);
ObjectInputStream ois=新ObjectInputStream(fis);
//下面是调试用的土司。
Toast.makeText(这个“Try块读取名为1的数据”,Toast.LENGTH_SHORT.show();
data=(List)ois.readObject();//错误原因。
//下面是用于调试的toast。但它从未执行过。
Toast.makeText(这个“Try块读取名为2的数据”,Toast.LENGTH_SHORT.show();
ois.close();
}catch(filenotfounde异常){
e、 printStackTrace();
Log.d(“标记A”,“未找到arrayListToday文件”);
}捕获(例外e){
Log.d(“标记A”,“生成异常1”);
}
返回数据;
}
public void addWater(View){//此方法将数据添加到recyclerView。单击浮动操作按钮可调用此方法
InformationToday i=新InformationToday();
if(view.getId()==R.id.50u按钮)
{
i、 volume=“50”;
Log.d(“标签A”,“i.volume=50”);
}
其他的
{
i、 volume=“100”;
}
数据。添加(0,i);
adapter.update(data);//此方法在recyclerView适配器中描述。它更新recyclerView数据。
试一试{
fos=getBaseContext().openFileOutput(“arrayListToday”,Context.MODE_PRIVATE);
ObjectOutputStream oos=新的ObjectOutputStream(fos);
Toast.makeText(这是“Try block for wrtie data called 1”,Toast.LENGTH_SHORT).show();//用于调试。
oos.writeObject(数据);//错误原因。
Toast.makeText(这个“Try块用于写入名为2的数据”,Toast.LENGTH_SHORT).show();//用于调试。它永远不会执行。
oos.close();
}catch(filenotfounde异常){
Toast.makeText(这是“Catch:File not gound exception”,Toast.LENGTH_SHORT.show();
Log.d(“标记A”,“捕获:未找到文件”);
e、 printStackTrace();
}捕获(例外e){
Toast.makeText(这个“Catch:Exception e”,Toast.LENGTH_SHORT.show();
Log.d(“标记A”,“捕获:异常e”);
}
com.getbase.floatingactionbutton.FloatingActionsMenu f=(FloatingActionsMenu)findViewById(R.id.multiple_actions);
f、 立即折叠();
}
}
没有这样的错误。但是 Logcat的可能异常:“未知错误(代码14)无法打开数据库。”2。此处不支持读取空字符串“3”。列上下文\u id不唯一(代码19)


所以,解决错误的问题(代码14)无法打开数据库并没有帮助

您必须序列化对象,然后将它们写入文件中

保存(无异常处理代码):

试试这个,让我知道这是否适合你


为什么不使用数据库,但问题与database@bpA遗憾的是,到目前为止,我还没有数据库方面的经验。我的应用程序也不需要太多的数据供将来参考。是的,我对发展还不熟悉。是的,我将很快使用数据库,但目前,需要按原样解决它。:)我花了一些时间来理解可序列化的,但现在终于可以工作了。蒂纳克斯
FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(data);
os.close();
fos.close();