空指针异常ContextWrapper

编程入门 行业动态 更新时间:2024-10-26 00:30:22
本文介绍了空指针异常ContextWrapper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一类称为FileGeneration扩展活动 在FileGeneration我有一个名为方法

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; }

和我得到的logcat这个输出

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)

278线的类

Line 278 in the class is

this.os = openFileOutput(this.fileName, cxt);

但是,当我刚刚与paramters打印方法在logcat中,它说

But when I just print the method with the paramters out in Logcat it says

openFileOutput(Preferences.xml, 1);

该文件不存在,但openFileOutput表示,将创建它,如果它不存在

The file does not exist, but openFileOutput says it will create it, if it does not exist

什么是错的?

推荐答案

我解决了这个问题,(新ContextWrapper(CTX))。openFileOutput(this.fileName,CXT)

更多推荐

空指针异常ContextWrapper

本文发布于:2023-08-02 17:52:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1279881.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   异常   ContextWrapper

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!