ContextWrapper 中的 NullPointer 异常
我有一个名为 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;
}
中得到此输出
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)
,我在 Logcat类中的第 278 行
this.os = openFileOutput(this.fileName, cxt);
,但是当我在 Logcat 中打印带有参数的方法时,它说
openFileOutput(Preferences.xml, 1);
文件不存在,但 openFileOutput 说它将创建它,如果它不存在
有什么问题吗?
I have a class called FileGeneration that extends Activity
In FileGeneration I have a method called
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;
}
And I get this output in 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)
Line 278 in the class is
this.os = openFileOutput(this.fileName, cxt);
But when I just print the method with the paramters out in Logcat it says
openFileOutput(Preferences.xml, 1);
The file does not exist, but openFileOutput says it will create it, if it does not exist
What can be wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我用
(new ContextWrapper(ctx)).openFileOutput(this.fileName, cxt)
解决了问题I solved the problem with
(new ContextWrapper(ctx)).openFileOutput(this.fileName, cxt)
尝试编写一个测试来查看是否
this.filename == null
,并使用Log.d(String,String)
方法输出结果。Try writing a test to see if
this.filename == null
, and output the result using theLog.d(String,String)
method.