该应用希望存储有关跳跃的数据(长度,高度,时间)
每当我尝试读取文件时,都会在“ File directory = context.getFilesDir();
”行中出现此异常
java.lang.NullPointerException:尝试调用虚拟方法 空对象上的'java.io.File android.content.Context.getFilesDir()' 参考”
我真的不知道要在上下文中添加什么,因此它不是null。
谢谢!
public class Save {
static private String savedData;
private static Context context;
static String stringJump;
public static void Save(Jump jump) {
savedData += jump.getLength() + " ";
savedData += jump.getHeight() + " ";
savedData += jump.getTime() + " ";
FileOutputStream outputStream;
String filename = "savedJump";
try {
outputStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(savedData.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
stringJump = read_file(context, filename);
Toast.makeText(context, stringJump, Toast.LENGTH_SHORT).show();
}
public static String read_file(Context context, String filename) {
File directory = context.getFilesDir();
File file = new File(directory, filename);
return file.toString();
}
}