2017-07-20 3 views
0

Je fais un projet qui affichera des notifications à l'utilisateur s'il est défini setLuminLevels()setHumidLevels()setTempLevels() renvoyer true.Pourquoi plusieurs utilisations de Notification.Builder bloquent-elles l'application?

Ceci est mon principal

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

Pourquoi cet accident de code mon application? Comment utiliser les notifications multi-fiches?

Crash Report:

            --------- 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

Avez-vous un journal sur ce crash? En passant, je crois que mNotifyMgr1, mNotifyMgr2, mNotifyMgr3 est la même instance, donc vous n'avez pas besoin de beaucoup de variables. – egoldx

+0

@ user1779222 Je n'en ai pas non, je vous tiendrai au courant quand je le ferai –

+0

@ user1779222 c'est ce que je pensais, mais comment générer des notifications en tant que nouvelles instances? Je pensais que ** new ** s'occupait de ces choses –

Répondre

1

Tenir compte de ces lignes de votre journal:

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) 

Vous avez un écouteur pour l'événement OnDataChange qui est appelé, et tente d'analyser une chaîne en un entier. La chaîne contient "26.57" qui n'est pas un nombre entier valide.

Regardez à la ligne 206 de votre MainActivity.java

PS: user1779222 est juste. Le NotificationManager est un singleton, vous n'avez donc besoin que d'une référence.

+0

merci, je rencontre d'autres problèmes maintenant, mais merci –