获取GCM实例ID的空指针

时间:2015-08-05 10:12:11

标签: android android-asynctask nullpointerexception google-cloud-messaging

我想将我的设备注册到我的gcm。在我的另一个应用程序中,此代码工作正常。但在这个应用程序中,此代码获取空指针。这是我的代码

private void registerInBackground(final String emailID) {
        new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... params) {
                String msg = "";
                try {
                    if (gcmObj == null) {
                        instanceID = InstanceID.getInstance(applicationContext);
                    }
                    regId = instanceID.getToken(ApplicationConstants.GOOGLE_PROJ_ID,
                            GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
                    msg = "Registration ID :" + regId;
                    try {
                        GcmPubSub.getInstance(getApplicationContext()).subscribe(regId, "/topics/global", null);
                    }catch (Exception e){
                        Toast.makeText(getApplicationContext(),e.getMessage().toString()+"Gagal Subcribe",Toast.LENGTH_LONG).show();
                    }
                } catch (IOException ex) {
                    msg = "Error :" + ex.getMessage();
                }
                return msg;
            }

            @Override
            protected void onPostExecute(String msg) {
                if (!TextUtils.isEmpty(regId)) {
                    // Store RegId created by GCM Server in SharedPref
                    SharedPreferences prefs = getSharedPreferences("UserDetails", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = prefs.edit();
                    editor.putString("regId",regId);
                    startActivity(new Intent(getApplicationContext(),Home.class));
                    finish();
                    /*Toast.makeText(
                            applicationContext,
                            "Registered with GCM Server successfully.\n\n"
                                    + msg, Toast.LENGTH_SHORT).show();*/
                } else {
                    Toast.makeText(
                            applicationContext,
                            "Reg ID Creation Failed.\n\nEither you haven't enabled Internet or GCM server is busy right now. Make sure you enabled Internet and try registering again after some time."
                                    + msg, Toast.LENGTH_LONG).show();
                }
            }
        }.execute(null, null, null);

    }

这是我的空指针异常。

08-05 16:58:19.741  26139-26464/? E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    Process: com.fingerspot.fingerspot, PID: 26139
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: java.lang.NullPointerException
            at com.google.android.gms.iid.InstanceID.zza(Unknown Source)
            at com.google.android.gms.iid.InstanceID.getInstance(Unknown Source)
            at com.fingerspot.fingerspot.Login$2.doInBackground(Login.java:145)
            at com.fingerspot.fingerspot.Login$2.doInBackground(Login.java:139)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)

我的代码出了什么问题?因为这个代码在我的另一个应用程序中运行正常,但是获取此应用程序的空指针。

2 个答案:

答案 0 :(得分:0)

使用getApplicationContext()而不是applicationContext

答案 1 :(得分:0)

由于您的applicationContext为空。在您的活动中,最好在onCreate方法中初始化它,如:

applicationContext = YourActivity.this;

或试试这个:

instanceID = InstanceID.getInstance(getApplicationContext());

或试试这个:

instanceID = InstanceID.getInstance(YourActivity.this);