【发布时间】:2011-09-11 01:23:24
【问题描述】:
我有一个名为 FileGeneration 的类,它扩展了 Activity
在 FileGeneration 我有一个方法叫做
protected OutputStream openAndWriteFile() {
// Set the Context-mode
int cxt = Context.MODE_PRIVATE;
// Check if we are not going to clear the file and the file exists
if (!clearFile && (new File(this.fileName)).exists()) {
// Append to the file
cxt = Context.MODE_APPEND;
}
// Try to open the file to write to
try {
// Open the File using the Context
this.os = openFileOutput(this.fileName, cxt);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Return the OutputStream
return this.os;
}
我在 Logcat 中得到这个输出
06-08 15:31:43.733: ERROR/AndroidRuntime(2850): java.lang.NullPointerException
06-08 15:31:43.733: ERROR/AndroidRuntime(2850): at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:158)
06-08 15:31:43.733: ERROR/AndroidRuntime(2850): at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:158)
06-08 15:31:43.733: ERROR/AndroidRuntime(2850): at dataconnection.FileGeneration.openAndWriteFile(FileGeneration.java:278)
类中的第 278 行是
this.os = openFileOutput(this.fileName, cxt);
但是当我在 Logcat 中打印带有参数的方法时,它会说
openFileOutput(Preferences.xml, 1);
文件不存在,但是openFileOutput说如果不存在就会创建它
有什么问题?
【问题讨论】:
标签: android android-activity nullpointerexception android-context