gpt4 book ai didi

android - GoogleCloudMessaging.getInstance(未知来源)NullPointer

转载 作者:行者123 更新时间:2023-11-29 01:34:04 25 4
gpt4 key购买 nike

我正在尝试从我的应用程序类 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com