java - Android 应用程序在尝试从文件读取数据时不断崩溃

标签 java android 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 - Android 应用程序在尝试从文件读取数据时不断崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23282686/

相关文章:

Java 异步 api 调用 - 即发即忘 http 调用

JavaFX标签不会持续更新

android - Proguard:如何避免缩小(和混淆)整个包以避免删除(和混淆) "unused methods"?

c - 如何让 LZO 处理文件流?

java - 在目录中创建文件之前检查目录中的写访问权限

python - 当另一个进程仍在写入时删除文件

java - 如何创建实时位置共享安卓应用

java - Firebase Java Query.equalTo() 不起作用

如果参数为真,Android Studio 会设置 onclicklistener

android - 在 Android 4.4+ 上运行时如何忽略 Cordova App 的字体大小设置更改?