有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么要多次使用通知。应用程序崩溃?

我正在制作一个项目,如果setsetLuminLevels(){}{}返回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.安卓.gms.internal.zzbmz.zza(Unknown Source)
                                                                                at com.google.安卓.gms.internal.zzbnz.zzYj(Unknown Source)
                                                                                at com.google.安卓.gms.internal.zzboc$1.run(Unknown Source)
                                                                                at 安卓.os.Handler.handleCallback(Handler.java:751)
                                                                                at 安卓.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at 安卓.os.Looper.loop(Looper.java:154)
                                                                                at 安卓.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:776)

共 (1) 个答案

  1. # 1 楼答案

    从日志中考虑这些行:

    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”不是有效的整数

    看看你主要活动的第206行。爪哇

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