java - 为什么多次使用Notification.Builder会使应用程序崩溃?

标签 java android notificationmanager

我正在制作一个项目,如果设置 setLuminLevels() setHumidLevels() setTempLevels() 返回 true,则会向用户显示通知。

这是我的主要内容

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        NotificationCompat.Builder lumin =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.index)
                        .setContentTitle("Greenhouse Warning:")
                        .setContentText("Luminosity Out of range!");

        NotificationCompat.Builder humid =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.index)
                        .setContentTitle("Greenhouse Warning:")
                        .setContentText("Humidity Out of range!");

        NotificationCompat.Builder temp =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.index)
                        .setContentTitle("Greenhouse Warning:")
                        .setContentText("Temperature Out of range!");

        int mNotificationId1 = 001;
        int mNotificationId2 = 002;
        int mNotificationId3 = 003;

        NotificationManager mNotifyMgr1 = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationManager mNotifyMgr2 = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationManager mNotifyMgr3 = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        //set and check the displayed levels
        if(setLuminLevels()) mNotifyMgr1.notify(mNotificationId1, lumin.build());
        if(setHumidLevels()) mNotifyMgr2.notify(mNotificationId2, humid.build());
        if(setTempLevels() ) mNotifyMgr3.notify(mNotificationId3, temp.build() );
    }

为什么这段代码会让我的应用程序崩溃?如何使用多重通知?

崩溃报告:

                                                    --------- beginning of crash
07-20 14:23:57.136 2645-2645/com.joanjantz_lee.greenhouse E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.joanjantz_lee.greenhouse, PID: 2645
                                                                            java.lang.NumberFormatException: For input string: "26.57"
                                                                                at java.lang.Integer.parseInt(Integer.java:521)
                                                                                at java.lang.Integer.parseInt(Integer.java:556)
                                                                                at com.joanjantz_lee.greenhouse.MainActivity$3.onDataChange(MainActivity.java:206)
                                                                                at com.google.android.gms.internal.zzbmz.zza(Unknown Source)
                                                                                at com.google.android.gms.internal.zzbnz.zzYj(Unknown Source)
                                                                                at com.google.android.gms.internal.zzboc$1.run(Unknown Source)
                                                                                at android.os.Handler.handleCallback(Handler.java:751)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:154)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

最佳答案

考虑日志中的这些行:

java.lang.NumberFormatException: For input string: "26.57"
at java.lang.Integer.parseInt(Integer.java:521)                                                                                
at java.lang.Integer.parseInt(Integer.java:556)                                                                                
at com.joanjantz_lee.greenhouse.MainActivity$3.onDataChange(MainActivity.java:206)

您有一个正在调用的 onDataChange 事件的监听器,并且正在尝试将字符串解析为整数。该字符串包含“26.57”,这不是一个有效的整数。

查看 MainActivity.java 的第 206 行

PS:user1779222 是对的。 NotificationManager 是一个单例,因此您只需要对它进行一次引用。

关于java - 为什么多次使用Notification.Builder会使应用程序崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45221186/

相关文章:

java - 如何在Java中使用PrintWriter和File类?

java - 线程安全 - 传递给线程的最终局部方法变量?

Java - ArrayList 默认初始值

android - 应用程序持久数据路径失败

android - NotificationManager getActiveNotifications() 适用于旧设备

java - 如何同步通知生成器和警报管理器?

java - 未找到 Spring 应用程序上下文

android - Firebase 在线到 Android 离线数据存储

java - Retrofit 2.0,Jackson 和多态性 : Could not find creator property with name <. ..>

状态栏中的Android Seek Bar