Domanda

My app have a service that displays push notifications, the thing is that i need to trigger different notifications depending on what kind of data received, to do so i have created 3 functions for notifications.

The problem lays on how to trigger those notifications, i'm using this:

       String salaId = "";
    String message = "";
    String quien = "";
    String iduser = "";
    String link = "";
    String img = "";

    protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "Mensaje recibido");
    message = intent.getExtras().getString("message");
    quien = intent.getExtras().getString("quien");
    salaId = intent.getExtras().getString("sala");
    iduser = intent.getExtras().getString("iduser");
    img = intent.getExtras().getString("foto");
    link = intent.getExtras().getString("link");

    if (salaId != null || !salaId.equals("") && img != null || !img.equals("")) {
        generateNotification(context, message, quien, salaId, iduser, img);
    }

    if (link != null || !link.equals("")) {
        generateNotification2(context, message, quien, link);
    }
    if (link == null || link.equals("") && salaId == null || salaId.equals("") && img == null || img.equals("")) {
        generateNotification3(context, message, quien);
    }
}

But the app dies when trying to select the right function:

04-30 01:47:10.716: E/AndroidRuntime(15106): FATAL EXCEPTION: IntentService[GCMIntentService-712601586692-2]
04-30 01:47:10.716: E/AndroidRuntime(15106): java.lang.NullPointerException
04-30 01:47:10.716: E/AndroidRuntime(15106):    at com.gettford.community.GCMIntentService.onMessage(GCMIntentService.java:72)
04-30 01:47:10.716: E/AndroidRuntime(15106):    at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:223)
04-30 01:47:10.716: E/AndroidRuntime(15106):    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
04-30 01:47:10.716: E/AndroidRuntime(15106):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 01:47:10.716: E/AndroidRuntime(15106):    at android.os.Looper.loop(Looper.java:130)
04-30 01:47:10.716: E/AndroidRuntime(15106):    at android.os.HandlerThread.run(HandlerThread.java:60)

In line 72 is this code:

if (salaId != null || !salaId.equals("") && img != null || !img.equals("")) {
È stato utile?

Soluzione

You said line 72 contains this code:

if (salaId != null || !salaId.equals("") && img != null || !img.equals(""))

If so, then either salaId or img is null. Use TextUtils.isEmpty() in your condition to handle both empty and null cases:

if (!TextUtils.isEmpty(salaId) && !TextUtils.isEmpty(img)) 

Altri suggerimenti

Make sure you check for all the strings for its null value whichever you are passing in your generateNotification() method as below:

Try using the TextUtils.isEmpty() method which will return true or false if the string is null or zero. Try out as below:

if (!TextUtils.isEmpty(salaId) && !TextUtils.isEmpty(img) || !TextUtils.isEmpty(message) || !TextUtils.isEmpty(quien) || !TextUtils.isEmpty(iduser) ) {
        generateNotification(context, message, quien, salaId, iduser, img);
    }

Hope this helps you.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top