在以下代码中:
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。为什么?
我发现此异常特别令人困惑,因为Google声明“您通过在Activity或其他应用程序组件的实现中调用getContentResolver()来获取ContentResolver”
http://developer.android.com/guide/topics/providers/content-providers.html
答案 0 :(得分:4)
在覆盖oncreate时比在构造函数中更好地执行此操作。我想你的应用还没有上下文。
实际上,这就是我昨天为一些LVL代码所做的事情:
/** Called when the activity is first created. */
@Override
public void onCreate() {
super.onCreate();
LICENSED_APP_ID = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
}//cons
它就像一个魅力......
答案 1 :(得分:0)
“Application”类不是“Application Component”。 http://developer.android.com/guide/topics/fundamentals.html#Components
要解决此问题,我的计划是从服务中获取ANDROID_ID。