20

My android app crashes and this is the logcat :-

java.lang.NullPointerException
    at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:194)
    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.os.HandlerThread.run(HandlerThread.java:60)

I looked into android gcm r3 source and I found that the argument intent is null in onHandleIntent().

Is this even possible? How to fix it?

(I know null intent could be seen with Service.onStartCopmmand returning START_STICKY but IntentService.onStartCommand doesn't use START_STICKY.)

NoraBora
  • 333
  • 3
  • 8
  • How can you say that the intent is null?? Focus on line 194 in your onHandleIntent(), there might be somthing else which is getting null... – umair.ali Apr 04 '13 at 11:13
  • Thank you for reply. Because I have the jar and source from android sdk. line 194 is "String action = intent.getAction();" – NoraBora Apr 05 '13 at 01:15
  • The intent is null on some systems and it is totally normal in case msg.obj that gets passed is null. There is no null check there. – Edison Jun 02 '13 at 06:57
  • Consider adding your voice to this bug report https://code.google.com/p/gcm/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Priority%20Milestone%20Owner%20Summary&groupby=&sort=&id=20 – larham1 Sep 06 '13 at 15:59
  • Possible duplicate of [IntentService's onHandleIntent(Intent) gets null argument](http://stackoverflow.com/questions/22309582/intentservices-onhandleintentintent-gets-null-argument) – rds Jan 26 '16 at 16:42

2 Answers2

1

I think application crashes only in some devices during installation time. It's because during installation GCM service also receives some Intent from other Google source and your broadcast receiver is not prepared to handle this type of Intent.

If you just want to receive GCM Intent which you want to pull from server through push notification, then just use this in your handle Intent call.

protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
        //String msg = intent.getStringExtra("message");
        String from=extras.getString("from");
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {

            if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                sendErrorNotification("Send error: " + extras.toString());
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
                sendErrorNotification("Deleted messages on server: " + extras.toString());
                // If it's a regular GCM message, do some work.
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                // This loop represents the service doing some work.
                for (int i = 0; i < 5; i++) {
                    Log.i(TAG, "Working... " + (i + 1) + "/5 @ " + SystemClock.elapsedRealtime());
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                    }
                }
                Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                // Post notification of received message.
                // sendNotification("Received: " + extras.toString());
                /*********ERROR IN SOME DEVICES*****************/

                 if(from.equals("google.com/iid"))
                 {
                     //related to google ... DO NOT PERFORM ANY ACTION
                 }
                 else { 
                   //HANDLE THE RECEIVED NOTIFICATION
                     String msg = intent.getStringExtra("message");
                     sendNotification(msg);
                    Log.i(TAG, "Received: " + extras.toString());
                     }
                 /**************************/
            }
        }
        GcmBroadcastReceiver.completeWakefulIntent(intent);
}
Micho
  • 3,929
  • 13
  • 37
  • 40
amit
  • 171
  • 1
  • 9
  • I agree, my GCMBroadcastReceiver receives some null intent from time to time, so I have to check if the incoming intent is null or not. I don't know what happens, but I get a null intent during installation. – Lev Feb 23 '16 at 15:00
  • Any update on this how to solve and check this issue? – Nisarg Thakkar Jul 03 '17 at 11:57
0

Happend to me once, I was passing wrong URI. Reference the documnetation for the intent u wanna pass.

Intent Documentation here.

StabCode
  • 106
  • 1
  • 6