开发者

Writing Data to Internal File - ContextWrapper NullPointerException

I'm creating a file for internal storage, and I'm running into a problem when the data is written. I know that it retrieves the right data when it's called (I used a Logcat tag to check that it was), but as soon as it tries to open the FileOutputStream, it says that there is a NullPointerException on the second line:

static ContextWrapper wrap = new ContextWrapper(context);
FileOutputStream gamesave = wrap.openFileOutput (FILENAM开发者_开发问答E, Context.MODE_PRIVATE);
gamesave.write(DATA.getBytes());
gamesave.close();

I've looked at other questions and I can't figure out why the NullPointerException is there, it seems to be following the right procedure.


I think you have the wrong Context in your context var (i.e. ApplicationContext). Try using the context directly - without the Wrapper.


Why are you creating a ContextWrapper? There is no reason to do that. Just use the Context you have at hand -- the Activity if you are an Activity, or one passed in to your code if not.

Note that it is perfectly fine to use the Application as a Context for this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜