Android GoogleCloudMessaging.getInstance(未知源)空指针
我正在尝试从我的应用程序类Global调用Google Cloud Messaging的注册操作,如下所示:Android GoogleCloudMessaging.getInstance(未知源)空指针,android,nullpointerexception,google-cloud-messaging,Android,Nullpointerexception,Google Cloud Messaging,我正在尝试从我的应用程序类Global调用Google Cloud Messaging的注册操作,如下所示: Global global; global = new Global(); // Adding GCM Registration actions: if (TextUtils.isEmpty(global.regId)) { global.regId = global.registerGCM(); Log.d("Regi
Global global;
global = new Global();
// Adding GCM Registration actions:
if (TextUtils.isEmpty(global.regId)) {
global.regId = global.registerGCM();
Log.d("RegisterActivity", "GCM RegId: " + Global.regId);
}
但每次我尝试运行时,都会出现以下错误:
Caused by: java.lang.NullPointerException
at GoogleCloudMessaging.getInstance(Unknown Source)
at com.example.infrastructure.global.Global.registerGCM(Global.java:62)
at com.example.infrastructure.SigningUp.onCreate(SigningUp.java:93)
在线global.regId=global.registerGCM()代码>
和行:gcm=GoogleCloudMessaging.getInstance(this)代码>
在我的应用程序类中,包含GCM注册操作(如下)
如何实现该应用程序类以允许使用这些GCM操作
全局应用程序类:
public class Global extends Application {
// Testing for Google Cloud Messaging
GoogleCloudMessaging gcm;
Context context;
public static String regId;
public static final String REG_ID = "regId";
private static final String APP_VERSION = "appVersion";
static final String TAG = "Register Activity";
public String registerGCM() {
gcm = GoogleCloudMessaging.getInstance(this);
regId = getRegistrationId(context);
if (TextUtils.isEmpty(regId)) {
registerInBackground();
Log.d("RegisterActivity",
"registerGCM - successfully registered with GCM server - regId: "
+ regId);
} else {
Toast.makeText(getApplicationContext(),
"RegId already available. RegId: " + regId,
Toast.LENGTH_LONG).show();
}
return regId;
}
private String getRegistrationId(Context context) {
final SharedPreferences prefs = getSharedPreferences(
MainActivity.class.getSimpleName(), Context.MODE_PRIVATE);
String registrationId = prefs.getString(REG_ID, "");
if (registrationId.isEmpty()) {
Log.i(TAG, "Registration not found.");
return "";
}
int registeredVersion = prefs.getInt(APP_VERSION, Integer.MIN_VALUE);
int currentVersion = getAppVersion(context);
if (registeredVersion != currentVersion) {
Log.i(TAG, "App version changed.");
return "";
}
return registrationId;
}
private static int getAppVersion(Context context) {
try {
PackageInfo packageInfo = context.getPackageManager()
.getPackageInfo(context.getPackageName(), 0);
return packageInfo.versionCode;
} catch (NameNotFoundException e) {
Log.d("RegisterActivity",
"I never expected this! Going down, going down!" + e);
throw new RuntimeException(e);
}
}
private void registerInBackground() {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String msg = "";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
regId = gcm.register(Config.GOOGLE_PROJECT_NUMBER);
Log.d("RegisterActivity", "registerInBackground - regId: "
+ regId);
msg = "Device registered, registration ID=" + regId;
storeRegistrationId(context, regId);
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
Log.d("RegisterActivity", "Error: " + msg);
}
Log.d("RegisterActivity", "AsyncTask completed: " + msg);
return msg;
}
@Override
protected void onPostExecute(String msg) {
Toast.makeText(getApplicationContext(),
"Registered with GCM Server." + msg, Toast.LENGTH_LONG)
.show();
}
}.execute(null, null, null);
}
private void storeRegistrationId(Context context, String regId) {
final SharedPreferences prefs = getSharedPreferences(
MainActivity.class.getSimpleName(), Context.MODE_PRIVATE);
int appVersion = getAppVersion(context);
Log.i(TAG, "Saving regId on app version " + appVersion);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(REG_ID, regId);
editor.putInt(APP_VERSION, appVersion);
editor.commit();
}
}
公共类全局扩展应用程序{
//谷歌云消息的测试
谷歌云通讯gcm;
语境;
公共静态字符串regId;
公共静态最终字符串REG_ID=“regId”;
私有静态最终字符串APP_VERSION=“appVersion”;
静态最终字符串TAG=“注册活动”;
公共字符串寄存器gcm(){
gcm=GoogleCloudMessaging.getInstance(this);
regId=getRegistrationId(上下文);
if(TextUtils.isEmpty(regId)){
registerInBackground();
Log.d(“RegisterActivity”,
“registerGCM-已成功向GCM服务器注册-regId:”
+雷吉德);
}否则{
Toast.makeText(getApplicationContext(),
“RegId已可用。RegId:+RegId,
Toast.LENGTH_LONG).show();
}
返回注册表;
}
私有字符串getRegistrationId(上下文){
最终SharedPreferences=getSharedPreferences(
MainActivity.class.getSimpleName(),Context.MODE_PRIVATE);
String registrationId=prefs.getString(REG_ID,“”);
if(registrationId.isEmpty()){
Log.i(标记“未找到注册”);
返回“”;
}
int registeredVersion=prefs.getInt(APP\u版本,Integer.MIN\u值);
int currentVersion=getAppVersion(上下文);
if(registeredVersion!=当前版本){
Log.i(标记“应用程序版本已更改”);
返回“”;
}
返回注册ID;
}
私有静态int getAppVersion(上下文){
试一试{
PackageInfo PackageInfo=context.getPackageManager()
.getPackageInfo(context.getPackageName(),0);
返回packageInfo.versionCode;
}catch(nameNotFounde异常){
Log.d(“RegisterActivity”,
“我从来没想到会这样!倒下,倒下!”+e);
抛出新的运行时异常(e);
}
}
私有无效注册表背景(){
新建异步任务(){
@凌驾
受保护字符串doInBackground(无效…参数){
字符串msg=“”;
试一试{
如果(gcm==null){
gcm=GoogleCloudMessaging.getInstance(上下文);
}
regId=gcm.register(Config.GOOGLE\u项目号);
Log.d(“RegisterActivity”、“registerBackground-regId:”
+雷吉德);
msg=“设备已注册,注册ID=“+regId;
storeRegistrationId(上下文,regId);
}捕获(IOEX异常){
msg=“错误:”+ex.getMessage();
Log.d(“RegisterActivity”,“Error:+msg”);
}
Log.d(“RegisterActivity”,“AsyncTask completed:”+msg);
返回味精;
}
@凌驾
受保护的void onPostExecute(字符串msg){
Toast.makeText(getApplicationContext(),
“已向GCM服务器注册。”+msg,Toast.LENGTH\u LONG)
.show();
}
}.执行(空,空,空);
}
私有void storeRegistrationId(上下文上下文,字符串regId){
最终SharedPreferences=getSharedPreferences(
MainActivity.class.getSimpleName(),Context.MODE_PRIVATE);
int-appVersion=getAppVersion(上下文);
Log.i(标记“在应用程序版本上保存注册表”+应用程序版本);
SharedReferences.Editor=prefs.edit();
编辑器.putString(REG_ID,regId);
编辑器.putInt(应用程序版本,应用程序版本);
commit();
}
}
看起来会对您有所帮助。在调用getRegistrationId(context)
时,请验证您的context
对象不为null这正是问题所在。我从另一个活动调用方法,但将上下文
引用留在应用程序类中,因此引用了一个非活动类,该类为上下文返回了null
。我所做的更改是将Context-Context
参数传递到方法中,以便活动类只需将getApplicationContext
传递到方法中