openFileOutput中的空指针 [英] null pointer in openFileOutput

查看:123
本文介绍了openFileOutput中的空指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从非Activity类的类中使用openFileOutput.当我写下面的东西时,它给了我空指针异常-

I'm trying to use openFileOutput from a class which is not Activity class. When I'm writing something following, it gives me null pointer exception-

try {
            Context con = null;
            fosCAM = con.openFileOutput(camFile, Context.MODE_PRIVATE);
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }    

有人可以帮我吗?

推荐答案

由于将Context变量con设置为null,然后使用con.openFileOutput进行引用,因此您会收到null指针异常.

You're receiving a null pointer exception because you're setting the Context variable con to null and then referencing it with con.openFileOutput.

您在活动中在哪里使用此代码?

Where are you using this code, in an activity?

如果此代码在您的Activity中,则只需删除Context变量并调用openFileOutput.您可以执行此操作,因为活动是从上下文派生的.如果代码在另一个类中,则应将上下文传递给该类并使用它.

If this code is in your Activity, just remove the Context variable and call openFileOutput. You can do this because Activity derives from Context. If the code is in another class you should pass a context into the class and use it.

这篇关于openFileOutput中的空指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆