- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从我的应用程序类 Global 中调用 Google Cloud Messaging 的注册操作:
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();
}
}
最佳答案
看起来像this会帮助你。在调用 getRegistrationId(context)
时验证您的 context
对象不为 null。
关于android - GoogleCloudMessaging.getInstance(未知来源)NullPointer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29617317/
我正在尝试在我的项目中实现 gcm,因为我想制作设备到设备的消息传递应用程序 我在这里阅读 http://developer.android.com/google/gcm/client.html因为它
正如问题所说,如何找出 registration ID 何时执行?已在 GoogleCloudMessaging 中失效应用程序接口(interface)?我已经阅读了关于类似主题的几个问题的答案:D
我需要在我的应用中使用 GoogleCloudMessaging。我通过 Cocoapods 安装了它。当我添加这部分代码时: GLInstanceIDConfig *instanceIDConfig
我是世界上唯一遇到此问题的人。所以我认为我真的需要一位专家。 我正在使用 Eclipse 和 Android 开发工具。 我已将所有相关库添加到项目中 我已经添加了 gcm.jar GoogleClo
我正在使用 Google Cloud Messaging在我的应用程序中借助 Urban Airship。 Urban Airship 正在向 GCM 注册,我的代码正在接收 GCM 消息。 我的问题
我试图在 Android 应用程序中实现推送通知,我发现了很多示例,但它们都导入了 com.google.android.gcm - 一个已被弃用的包 (据推测)它的功能包含在 GooglePlayS
我尝试从 gcm.jar 更新以从 google-play-services.jar 获取 GCM。我使用与所示相同的代码 here . 我使用的是与更改客户端实现之前相同的服务器 (node-gcm
我知道有很多帖子有同样的问题,但看完所有帖子后我找不到问题的原因。 我编写了一个 GCM 客户端来注册我的设备以从我的服务器接收消息。它正在运行,我可以将注册 ID 存储在我的数据库中。 我的问题出在
是否可以借助 robolectric 对 GCM 上游消息进行单元测试?这是我的单位: public void sendUpstream(Bundle data) { GoogleCloudM
我正在尝试让 GCM 在我的应用程序中运行(以在我们的营业时间更改或我们有任何促销 Activity 时通知用户),但我不断收到错误 无法解决尝试使用 Google Cloud Messaging A
我已经使用 Google Cloud Messaging 创建了 TokenID。 在我的 app 中有一个启用和禁用 Notification 的选项。 如果我使用 GoogleCloudMessa
我正在尝试从我的应用程序类 Global 中调用 Google Cloud Messaging 的注册操作: Global global; global = new Global();
我对 Java 编程和一般编程还很陌生。现在我决定制作我自己的应用程序,该应用程序应该使用谷歌云消息传递。不知何故我成功了,但后来我意识到我使用了方法 String regid = gcm.regis
我遇到了 android 应用程序所说的问题。 “The application xxxx Sorry stopped”与推送通知一起工作,当执行调试器时发生在它通过时: 方法一 InstanceID
我正在尝试发帖到 Googles Cloud Messaging Api (GCM) ,但我的请求失败,响应为 HTTP/1.1 400 InvalidTokenFormat。 但是,如果我更改程序,
最近,我注意到,Google 通过使用 google play service SDK,准确地说是 GoogleCloudMessaging 类,为 GCM 提供了新方法。我在 Google Code
我正在尝试使用 GCM 向设备发送消息。作为一种特殊情况,我使用 android 设备作为我的第三方服务器。我添加了以下代码,但出现“Unauthorized Error 401”。在这里,我只是想在
我正在使用 gcm 描述的新方法 here使用 GoogleCloudMessaging 类。但是 10 次中有 9 次出现 SERVICE_NOT_AVAILABLE 错误。只有一次它注册了一个设备
这个问题在这里已经有了答案: Is there any reason to continue using IntentService for handling GCM messages? (1 个回
你好,我正在按照 android 的官方文档发送推送通知,因为旧方法已被弃用,但我在实现 GCM 客户端时遇到问题 http://developer.android.com/google/gcm/cl
我是一名优秀的程序员,十分优秀!