本文介绍了为什么应用事业的NullPointerException内确实getContentResolver()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在以下code:
public class ApplicationContext extends Application
{
private static ApplicationContext instance;
public ApplicationContext()
{
instance = this;
final String strID = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
}
public static Context getContext()
{
return instance;
}
}
getContentResolver()会导致一个NullPointerException异常。为什么呢?
getContentResolver() causes a NullPointerException. Why ?
我觉得这是特别异常混乱,因为谷歌美国的 的
I find this exception especially confusing because Google states "You get a ContentResolver by calling getContentResolver() from within the implementation of an Activity or other application component"
推荐答案
覆盖OnCreate中比在构造函数时,更好地做到这一点。我猜你的应用程序没有上下文呢。
Do this when overriding oncreate better than in your constructor. I guess your app doesn't have a context yet.
其实,这里是我昨天做了一段LVL code:
Actually, here is what I did yesterday for some LVL code :
/** Called when the activity is first created. */
@Override
public void onCreate() {
super.onCreate();
LICENSED_APP_ID = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
}//cons
和它的工作原理就像一个魅力...
And it works like a charm...
这篇关于为什么应用事业的NullPointerException内确实getContentResolver()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!