获取 GCM 实例 ID 的空指针
Get Null Pointer for GCM Instance ID
我想将我的设备注册到我的 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.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.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.doInBackground(Login.java:145)
at com.fingerspot.fingerspot.Login.doInBackground(Login.java:139)
at android.os.AsyncTask.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.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)
我的代码有什么问题?因为这段代码在我的另一个应用程序中工作正常,但是为这个应用程序获取空指针。
使用 getApplicationContext() 而不是 applicationContext
因为您的 applicationContext
为空。在您的 Activity 中,最好在 onCreate
方法中初始化它,例如:
applicationContext = YourActivity.this;
或者试试这个:
instanceID = InstanceID.getInstance(getApplicationContext());
或者试试这个:
instanceID = InstanceID.getInstance(YourActivity.this);
我想将我的设备注册到我的 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.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.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.doInBackground(Login.java:145)
at com.fingerspot.fingerspot.Login.doInBackground(Login.java:139)
at android.os.AsyncTask.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.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)
我的代码有什么问题?因为这段代码在我的另一个应用程序中工作正常,但是为这个应用程序获取空指针。
使用 getApplicationContext() 而不是 applicationContext
因为您的 applicationContext
为空。在您的 Activity 中,最好在 onCreate
方法中初始化它,例如:
applicationContext = YourActivity.this;
或者试试这个:
instanceID = InstanceID.getInstance(getApplicationContext());
或者试试这个:
instanceID = InstanceID.getInstance(YourActivity.this);