2017-07-20 3 views
0

Ich mache ein Projekt, das Benachrichtigungen an den Benutzer anzeigen wird, wenn setLuminLevels()setHumidLevels()setTempLevels() True gesetzt wird.Wieso stürzen mehrere Anwendungen auf Notification.Builder die App ab?

Das ist mein Haupt

@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()); 
    } 

Warum sollte dieser Code Absturz meine app? Wie verwende ich mehrere Benachrichtigungen?

Crash-Bericht:

            --------- 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) 
+1

Haben Sie ein Protokoll über diesen Absturz? Übrigens glaube ich, dass mNotifyMgr1, mNotifyMgr2, mNotifyMgr3 dieselbe Instanz ist, so dass Sie nicht so viele Variablen benötigen. – egoldx

+0

@ user1779222 Ich habe keine Nein, ich werde Sie benachrichtigt, wenn ich tue –

+0

@ user1779222 ist es, was ich dachte, aber wie würde ich Benachrichtigungen als neue Instanzen generieren? Ich dachte, dass ** neu ** sich um solche Dinge kümmerte –

Antwort

1

Betrachten Sie diese Zeilen aus dem Protokoll:

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) 

Sie haben einen Listener für das Ereignis OnDataChange, die aufgerufen wird, und versucht, eine Zeichenfolge in ein analysieren ganze Zahl. Die Zeichenfolge enthält "26.57", die keine gültige Ganzzahl ist.

Blick auf Zeile 206 Ihrer MainActivity.java

PS: user1779222 ist richtig. Der NotificationManager ist ein Singleton, so dass Sie nur einen Verweis darauf benötigen.

+0

danke, ich laufe jetzt in andere Probleme, aber danke –

Verwandte Themen