Android:找不到文件异常,它第一次工作了吗?

时间:2017-12-11 03:49:51

标签: java android

我正在执行一个项目,到目前为止,在该学科中,我们无法使用数据库来保存数据。我在.tmp文件中持久保存数据。我第一次坚持医生名单,但它确实有效,但现在我试图保留患者用户数据,但是这个错误发生了,找不到该文件。

这些是我的负载,以及类中的保存方法" SharedResources":

public void loadUserPatient(Context context) {
    FileInputStream fis1;
    try {

        fis1 = context.openFileInput("patient.tmp");

        ObjectInputStream ois = new
                ObjectInputStream(fis1);
        userPatient = (UserPatient) ois.readObject();
        ois.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch(IOException e) {
        e.printStackTrace();
    } catch(ClassNotFoundException e) {
        e.printStackTrace();
    }
}


public void saveUserPatient(Context context) {
    FileOutputStream fos1;
    try {
        fos1 = context.openFileOutput("patient.tmp",
                Context.MODE_PRIVATE);
        ObjectOutputStream oos =
                new ObjectOutputStream(fos1);
        oos.writeObject(userPatient);
        oos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

这是全班:https://ideone.com/f3c74u

错误发生在MainActivity的第16行:

SharedResources.getInstance().loadUserPatient(this);

这是全班"主要":https://ideone.com/OyiljP

我认为这个错误正在发生,因为UserPatientAdd类的第52行:

SharedResources.getInstance().getUserPatient(); 

因为当我使用ArrayList时,我在行的末尾添加了一个添加内容,例如:SharedResources.getInstance().getDoctors().add(doctor);

当我只与用户打交道时,我对如何继续感到困惑。 这是整个UserPatientAdd类:https://ideone.com/clUSa3

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您需要使用类似

的内容设置UserPatient

SharedResources课程中,创建一个新方法:

 public void setUserPatient(UserPatient user) {
        userPatient = user;
    }

然后在你的UserPatientAdd类中设置新对象:

 UserPatient userPatient = new UserPatient (birth, name, bloodType, bloodPressure, cbpm, vacinesTaken, vacinesToBeTaken,
            allergies,weight, height, surgeries, desease);


SharedResources.getInstance().setUserPatient(userPatient);

完成