Android ContextWrapper中的NullPointer异常
我有一个名为FileGeneration的类,它扩展了活动Android ContextWrapper中的NullPointer异常,android,android-activity,nullpointerexception,android-context,Android,Android Activity,Nullpointerexception,Android Context,我有一个名为FileGeneration的类,它扩展了活动 在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
在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表示,如果它不存在,它将创建它
有什么问题吗?尝试编写一个测试,看看
this.filename==null
,然后使用Log.d(String,String)
方法输出结果。我解决了(新ContextWrapper(ctx))的问题。openFileOutput(this.filename,cxt)
我使用Log.d(“ProgramName”,“openFileOutput(”+this.fileName+”,“+cxt+”);这就给出了问题的结果