简体   繁体   English

Android,应用程序因字符串崩溃

[英]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 . 如果是这样,则salaIdimgnull 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.

相关问题 由于 Android 中的数据存储迁移,应用程序升级后应用程序崩溃 - App crash after app upgrade due to data store migration in Android 由于gps权限拒绝,Android应用程序在Android 4.4上崩溃 - android app crash on android 4.4 due to gps permission denial 由于Android中的相机而崩溃 - crash due to camera in android 由于GoogleApiClient,Android崩溃 - Android crash due to GoogleApiClient Android-由于设置撤消了位置权限,导致应用崩溃 - Android - app crash due to location permission revoked from setting Android:应用启动时立即崩溃(由于房间数据库) - Android: Immediate Crash on App Launch (Due to Room Database) Android 应用程序因 Jetpack Compose 的 Accompanist HorizontalPager 中的 NullPointerException 而崩溃 - Android app crash due to NullPointerException in Accompanist HorizontalPager for Jetpack Compose Android App崩溃,无法将editText转换为String - Android App getting crash and not Converting editText to String 将片段与可绘制资源(jpg)结合使用时,由于内存分配错误导致android应用崩溃 - android app crash due to memory alloc error while using fragment with drawable resource (jpg) 由于位置客户端非法状态导致应用崩溃 - App Crash due to Location Client Illegal State
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM