簡體   English   中英

嘗試從文件讀取數據時,Android應用不斷崩潰

[英]Android app keeps crashing when trying to read data from file

我不知道為什么會這樣。 當全部放在一個文件中時,它正在工作。 也許應用程序上下文是錯誤的。 我似乎無法弄清楚。 謝謝。

userDataFile = this.getFilesDir().getPath().toString() + "/datafile.txt";
if (Helper.readUserFromFile(userDataFile) != null){
    Log.d("Before intent", "Prior to calling");
    startIntent();
}
Log.d("After if statement", "After");

//In Helper.readUserFromFile...
public class Helper {
    public static User readUserFromFile(String filePath){

FileInputStream courseFile = null;
File file = null;
ObjectInputStream courseObj = null;

try{
    file = new File(filePath);
    if (!file.exists()) {
            Log.d("noFile", "wtttttttf");
            Log.d("filepath", filePath + "");
            Log.d("fileee", file.exists() + "");
            return null;
    }

logcat的

// CRASHES RIGHT AFTER "fileee" and doesn't log anything else
04-24 21:14:18.720: E/Trace(20053): error opening trace file: No such file or directory (2)
04-24 21:14:18.751: D/noFile(20053): wtttttttf
04-24 21:14:18.751: D/filepath(20053): /data/data/com.example.tutorrow/files/datafile.txt
04-24 21:14:18.751: D/fileee(20053): false
04-24 21:14:18.751: D/AndroidRuntime(20053): Shutting down VM
04-24 21:14:18.751: W/dalvikvm(20053): threadid=1: thread exiting with uncaught exception (group=0x40dbd2a0)
04-24 21:14:18.759: E/AndroidRuntime(20053): FATAL EXCEPTION: main
04-24 21:14:18.759: E/AndroidRuntime(20053): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tutorrow/com.example.tutorrow.MainActivity}: java.lang.NullPointerException

“錯誤打開跟蹤文件:沒有這樣的文件或目錄(2)”

您嘗試打開的文件不存在。

“ java.lang.RuntimeException:無法啟動活動ComponentInfo {com.example.tutorrow / com.example.tutorrow.MainActivity}:java.lang.NullPointerException”。這可能在另一個線程中發生嗎?

得到它了。 我必須在要寫入文件的對象的類上實現Serializable。 感謝大家!

暫無
暫無

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

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