一、前言:
新的app项目沿用之前写好的代码,出现问题的场景时是,我要debug的时候登录界面输入用户名密码之后进入首页。程序崩掉了!!!
并且报错'android.content.res.Resources android.content.Context.getResources()' on a null object reference。在网上看了很多解决办法,最后成功了。
二、解决方案
他说在获取上下文的时候,为空。那么我们只需要给他加一个非空判断就好了
我原来的代码:
Resources res = BaseApplication.getAppContext().getResources();
_fun = fun;
_imageSource = imageSourceID;
_text = res.getString(textSourceID);
修改之后的代码:
if (BaseApplication.getAppContext() != null){
Resources res = BaseApplication.getAppContext().getResources();
_fun = fun;
_imageSource = imageSourceID;
_text = res.getString(textSourceID);
}
可以看到,我在获取上下文的时候做了非空判断。
三、总结:
写android已经有一段时间了,很多关于.....为空的,像这种情况我们只需要duidui他进行非空判断。