简体   繁体   English

尝试从文件读取数据时,Android应用不断崩溃

[英]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 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)" “错误打开跟踪文件:没有这样的文件或目录(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. 我必须在要写入文件的对象的类上实现Serializable。 Thanks everyone! 感谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:尝试将API中的数据加载到列表视图时应用崩溃 - Android: App crashing when trying to load Data from API into a listview 尝试从Firebase获取数据时Android应用程序崩溃 - Android app crashing when trying to get data from Firebase 尝试请求网页时,应用不断崩溃 - App keeps crashing when trying to request webpage 尝试在Android上使用Google Tasks API会使我的应用崩溃 - Trying to use Google Tasks API on android keeps my app crashing 尝试将多个布局堆叠到android应用中的活动中,不断崩溃 - Trying to stack multiple layouts into activity in android app, keeps crashing 使用后台方法从我的片段中读取 json 后,我的 android studio 应用程序不断崩溃 - My android studio app keeps crashing after using a background method to read a json from my fragment 尝试更改重新加载的Fragment中的FloatingActionButton的状态时,应用程序不断崩溃 - App keeps crashing when trying to change the state of a FloatingActionButton in a reloaded Fragment 从导航抽屉打开新活动时,我的Android应用不断崩溃 - My Android App Keeps Crashing When Opening A New Activity From My Navigation Drawer 当我打算从 Fragment 到新 Activity (Android) 时,我的应用程序不断崩溃 - My app keeps crashing when I intent from a Fragment to a new Activity (Android) 使用GoogleMap V2时,Android App不断崩溃 - Android App keeps crashing when using GoogleMap V2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM