问题
I tried to update the GCM services (Google cloud messages) libraries (from 7.5 to 8.3.0) in my Gradle project. But now, with this new version, i'm unable to launch my previous activity which was working perfectly before.
The code which handle the following error is :
Intent in = new Intent(this, MyGcmListenerService.class);
startService(in);
MyGcmListenerService.java :
public class MyGcmListenerService extends GcmListenerService
{
private static final String TAG = "MyGcmListenerService";
@Override
public void onMessageReceived(String from, Bundle data)
{
Log.w(TAG, "onMessageReceived");
}
The returned error :
FATAL EXCEPTION: AsyncTask #1
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
E/AndroidRuntime: at com.google.android.gms.gcm.GcmListenerService.zzo(Unknown Source)
E/AndroidRuntime: at com.google.android.gms.gcm.GcmListenerService.zza(Unknown Source)
E/AndroidRuntime: at com.google.android.gms.gcm.GcmListenerService$1.run(Unknown Source)
E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
E/AndroidRuntime: at java.lang.Thread.run(Thread.java:818)
Gradle files :
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.google.android.gms:play-services-gcm:8.3.0'
}
If I rollback to the previous GCM version (7.5) it's working back as expected. Do you know what changed ? I checked the changelogs but I'm unable to found any informations about it. https://developers.google.com/android/guides/releases
Thank you for your help
回答1:
Try looking at the decompiled class file for GcmListenerService
using your IDE. When I do that with Android Studio for version 8.3.0, the code that appears to be throwing the exception is attempting to get the ACTION from the intent that invoked the service. Because you are invoking the service with an explicit intent, the ACTION is null.
I'm wondering why you are invoking your subclass of GcmListenerService
explicitly? The normal GCM processing for message receipt is for the message to be delivered to GcmReceiver
, which then passes it to the app's instance of GcmListenerService
for processing. You should not be invoking your listener service explicitly, GcmReceiver
does that. Take a look at the sample project.
来源:https://stackoverflow.com/questions/33692070/gcm-update-7-5-to-8-3-0-fatal-exception