GoogleCloudMessaging.getInstance(未知来源)NullPointer

时间:2015-04-14 00:37:33

标签: 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("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寄存器操作(如下所示)。

如何实现此Application类以允许使用这些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();
    }

}

1 个答案:

答案 0 :(得分:1)

看起来this会对您有所帮助。调用context时,请验证您的getRegistrationId(context)对象是否为空。