简体   繁体   中英

Android app keeps crashing when trying to read data from file

I don't know why this happens. It was working when it was all in one file. Maybe the application context is wrong. I can't seem to figure this out. Thanks.

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

"error opening trace file: No such file or directory (2)"

The file you are trying to open doesn't exist.

“ java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.tutorrow / com.example.tutorrow.MainActivity}:java.lang.NullPointerException”。这可能在另一个线程中发生吗?

Got it. I had to implement Serializable on the class of the Object I am writing to the file. Thanks everyone!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM