谷歌云Messaging.register给出空指针错误 [英] Google cloud Messaging.register gives null pointer error
问题描述
我实现了客户端谷歌云通讯
提示错误
这里
I implemented client for Google Cloud Messaging
it gives error
here
regid = gcm.register(PROJECT_NUMBER);
是空指针异常
推荐答案
我也对一些客户最近遇到了这个问题,在这里我得到了
i also encountered this problem recently on some clients, where i got
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.contains(java.lang.CharSequence)' on a null object reference
at com.google.android.gms.gcm.GoogleCloudMessaging.register(Unknown Source)
终于我可以重现它在一个新的安装程序模拟器中运行API级别23.关键部分是,当我想要注册的发件人ID。
finally i could reproduce it on a fresh setup emulator running api level 23. The critical part is when i want to register the sender id.
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
String regId = gcm.register(SENDER_ID);
在GCM对象不为空,但是当你看看在GoogleCloudMessaging.class文件寄存器()方法,该方法是这样的:
the gcm object is not null, but when you take a look at the register() method of the GoogleCloudMessaging.class file, the method looks like this:
public synchronized String register(String... senderIds) throws IOException {
String var2 = this.zzc(senderIds);
Bundle var3 = new Bundle();
if(zzaz(this.context).contains(".gsf")) {
var3.putString("legacy.sender", var2);
return InstanceID.getInstance(this.context).getToken(var2, "GCM", var3);
} else {
var3.putString("sender", var2);
Intent var4 = this.zzy(var3);
return zza(var4, "registration_id");
}
}
在这里似乎空指针来自。载有()检查。这似乎是如果设备上没有安装谷歌的播放服务的情况。但我不是100%肯定,如果这是主要原因。
and here it seems the nullpointer comes from the .contains() check. this seems to be the case if the google play services are not installed on a device. but i am not 100% sure if that is the main cause.
我最终通过捕捉在gcm.register()调用的空指针异常,并告诉用户安装谷歌播放服务固定的问题。
i ended up "fixing" the issue by catching the nullpointer exception on the gcm.register() call and telling the user to install the google play services.
此外,register()方法已经是德precated,所以也许这个问题本身的修补程序切换到使用实例id时:
furthermore the register() method is already deprecated, so maybe this issue fixes itself when switching over to using the InstanceID:
https://developers.google.com/cloud-messaging/registration
https://developers.google.com/instance-id/guides/ Android的实施
这篇关于谷歌云Messaging.register给出空指针错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!