簡體   English   中英

Android:找不到文件異常,它在第一次工作嗎?

[英]Android: File not found exception, it worked in the first time?

我正在執行一個項目,到目前為止,在該學科中,我們無法使用數據庫來持久化數據。 我將數據保存在.tmp文件中。 第一次我保留了醫生列表,並且它起作用了,但是現在我試圖保留患者用戶數據,但是發生此錯誤,找不到該文件。

這些是我的負載,還有“ SharedResources”類中的save方法:

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);

這是整個類“ Main”: https : //ideone.com/OyiljP

而且我認為由於UserPatientAdd類的第52行而發生此錯誤:

SharedResources.getInstance().getUserPatient(); 

因為當我使用ArrayList時,我在行的末尾添加了一個添加項,例如: SharedResources.getInstance().getDoctors().add(doctor);

當我只與用戶打交道時,我對如何繼續感到困惑。 這是整個UserPatientAdd類: https ://ideone.com/clUSa3

我怎么解決這個問題?

您需要使用類似以下內容來設置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);

完成

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM