我可以getFilesDir()
在使用的 .class 中使用该方法extends View
吗?
public void save(){
try {
File myDir = new File(getFilesDir().getAbsolutePath());
FileWriter fw = new FileWriter(myDir + "/Test.txt");
fw.write(Integer.toString(score));
fw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
只是该方法getFilesDir()
会收到一条错误消息,并提供一种快速修复方法 - create 方法getFilesDir()
。这是因为它在一个扩展 View 的类中,我可以通过将方法放在 Activity 类中并通过使用静态在 View 类中使用该方法来解决这个问题。这不起作用,程序崩溃并且分数不会被保存。