开发者

Writing Data to Internal File - ContextWrapper NullPointerException

开发者 https://www.devze.com 2023-03-20 18:27 出处:网络
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

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

精彩评论

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