[英]Android, app crash due to a string
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. 我的应用程序有一个显示推送通知的服务,我需要根据收到的数据类型触发不同的通知,为此我创建了3个通知功能。
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: 第72行是这段代码:
if (salaId != null || !salaId.equals("") && img != null || !img.equals("")) {
You said line 72 contains this code: 你说第72行包含这段代码:
if (salaId != null || !salaId.equals("") && img != null || !img.equals(""))
If so, then either salaId
or img
is null
. 如果是这样,则
salaId
或img
为null
。 Use TextUtils.isEmpty()
in your condition to handle both empty and null cases: 在条件中使用
TextUtils.isEmpty()
来处理空和空案例:
if (!TextUtils.isEmpty(salaId) && !TextUtils.isEmpty(img))
Make sure you check for all the strings for its null value whichever you are passing in your generateNotification()
method as below: 确保检查所有字符串的空值,无论您在
generateNotification()
方法中传递的是什么,如下所示:
Try using the TextUtils.isEmpty()
method which will return true or false if the string is null or zero. 尝试使用
TextUtils.isEmpty()
方法,如果字符串为null或零,则返回true或false。 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. 希望这对你有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.