2010-12-06 8 views
0

J'essaie d'écrire une application android simple pour aider à interpréter certains résultats de laboratoire.Comment gérer onClickListener dans android?

 
package org.android.bloodgas; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.TextView; 
import android.widget.Button; 

public class pinit extends Activity { 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Button interpretButton = (Button) findViewById(R.id.Interpret); 

     //interpretButton.setOnClickListener(this); 
     View.OnClickListener myhandler = new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        float inpPH, inpPO2, inpPCO2, inpHCO3, inpBE; 
        inpPH = Float.valueOf(findViewById(R.id.InpPH).toString()).floatValue(); 
        inpPO2 = Float.valueOf(findViewById(R.id.InpO2).toString()).floatValue(); 
        inpPCO2 = Float.valueOf(findViewById(R.id.InpCO2).toString()).floatValue(); 
        inpHCO3 = Float.valueOf(findViewById(R.id.InpHCO3).toString()).floatValue(); 
        inpBE = Float.valueOf(findViewById(R.id.InpBE).toString()).floatValue(); 

        TextView interpretation; 
        interpretation = (TextView) findViewById(R.id.Interpretation); 

        String sResult = null; 
        if (inpPH < 7.35) { 
         // Acidosis 
         sResult.concat("Acidosis. "); 
        } else { 
         if (inpPH > 7.45) { 
          // Alkalosis 
          sResult.concat("Alkalosis. "); 
         } else { 
          // Normal acidity, 7.35-7.45 
          sResult.concat("Normal pH. "); 
         } 
        } 
        interpretation.setText(sResult); 
      } 
     }; 

     interpretButton.setOnClickListener(myhandler); 

    } 

} 

Il ne compiler et exécuter, mais quand je presse sur le bouton « interpréter », il dit simplement l'application ... a arrêté de façon inattendue. Veuillez réessayer. (Fermeture forcée).

Je suis totalement nouveau à la programmation Android ... Donc je suppose que ce serait une erreur très stupide que j'ai faite, mais s'il vous plaît aider. Merci. Mise à jour: changé le code à ceci (moins la partie logique tandis que je compléterai plus tard) mais bloque toujours quand appuyer sur le bouton.

 
package org.android.bloodgas; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.Button; 

public class Pinit extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Button interpretButton = (Button) findViewById(R.id.Interpret); 

     View.OnClickListener myhandler = new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        float inpPH, inpPO2, inpPCO2, inpHCO3, inpBE; 
        inpPH = Float.valueOf(((EditText) Pinit.this.findViewById(R.id.InpPH)).toString()).floatValue(); 
        inpPO2 = Float.valueOf(((EditText) Pinit.this.findViewById(R.id.InpO2)).toString()).floatValue(); 
        inpPCO2 = Float.valueOf(((EditText) Pinit.this.findViewById(R.id.InpCO2)).toString()).floatValue(); 
        inpHCO3 = Float.valueOf(((EditText) Pinit.this.findViewById(R.id.InpHCO3)).toString()).floatValue(); 
        inpBE = Float.valueOf(((EditText) Pinit.this.findViewById(R.id.InpBE)).toString()).floatValue(); 
        // ... 
      } 
     }; 

     interpretButton.setOnClickListener(myhandler); 

    } 

} 

logcat:

 
12-07 03:37:02.231: WARN/dalvikvm(1074): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
12-07 03:37:02.332: ERROR/AndroidRuntime(1074): FATAL EXCEPTION: main 
12-07 03:37:02.332: ERROR/AndroidRuntime(1074): java.lang.NumberFormatException: [email protected] 
12-07 03:37:02.332: ERROR/AndroidRuntime(1074):  at org.apache.harmony.luni.util.FloatingPointParser.initialParse(FloatingPointParser.java:130) 
12-07 03:37:02.332: ERROR/AndroidRuntime(1074):  at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:319) 
12-07 03:37:02.332: ERROR/AndroidRuntime(1074):  at java.lang.Float.parseFloat(Float.java:291) 
12-07 03:37:02.332: ERROR/AndroidRuntime(1074):  at java.lang.Float.valueOf(Float.java:330) 
12-07 03:37:02.332: ERROR/AndroidRuntime(1074):  at org.android.bloodgas.Pinit$1.onClick(Pinit.java:22) 
12-07 03:37:02.332: ERROR/AndroidRuntime(1074):  at android.view.View.performClick(View.java:2408) 
12-07 03:37:02.332: ERROR/AndroidRuntime(1074):  at android.view.View$PerformClick.run(View.java:8816) 
12-07 03:37:02.332: ERROR/AndroidRuntime(1074):  at android.os.Handler.handleCallback(Handler.java:587) 
12-07 03:37:02.332: ERROR/AndroidRuntime(1074):  at android.os.Handler.dispatchMessage(Handler.java:92) 
12-07 03:37:02.332: ERROR/AndroidRuntime(1074):  at android.os.Looper.loop(Looper.java:123) 
12-07 03:37:02.332: ERROR/AndroidRuntime(1074):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
12-07 03:37:02.332: ERROR/AndroidRuntime(1074):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-07 03:37:02.332: ERROR/AndroidRuntime(1074):  at java.lang.reflect.Method.invoke(Method.java:521) 
12-07 03:37:02.332: ERROR/AndroidRuntime(1074):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
12-07 03:37:02.332: ERROR/AndroidRuntime(1074):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
12-07 03:37:02.332: ERROR/AndroidRuntime(1074):  at dalvik.system.NativeStart.main(Native Method) 
12-07 03:37:02.382: WARN/ActivityManager(68): Force finishing activity org.android.bloodgas/.Pinit 
12-07 03:37:02.952: WARN/ActivityManager(68): Activity pause timeout for HistoryRecord{44fbfc90 org.android.bloodgas/.Pinit} 
12-07 03:37:07.761: INFO/Process(1074): Sending signal. PID: 1074 SIG: 9 
12-07 03:37:09.852: INFO/ActivityManager(68): Process org.android.bloodgas (pid 1074) has died. 
12-07 03:37:09.862: INFO/WindowManager(68): WIN DEATH: Window{44fa71e0 org.android.bloodgas/org.android.bloodgas.Pinit paused=false} 
12-07 03:37:15.931: WARN/ActivityManager(68): Activity destroy timeout for HistoryRecord{44fbfc90 org.android.bloodgas/.Pinit} 
+0

Vous aurez besoin de publier une pile de votre logcat pour que nous puissions vous aider. – Falmarri

+0

mise à jour 2: Il s'avère que le coupable n'est pas sur le gestionnaire de clic. C'est le problème de conversion de type. Il s'avère que la valeur de chaîne de EditText est demandée via .getText() pas .toString(). Mon erreur stupide. – bubu

Répondre

3

La première chose que je vois est que vous devez lancer ce le type de vue qu'il est

inpPH = Float.valueOf(findViewById(R.id.InpPH).toString()).floatValue();

Deuxièmement, vous appelez ceci à partir de l'onclickListener. Donc, findViewById() ne devrait même pas compiler. Vous avez besoin

inpPH = Float.valueOf((TypeOfView) pinit.this.findViewById(R.id.InpPH).toString()).floatValue(); 

Et aussi en Java, les classes devraient commencer par lettors de capital, il devrait donc être

public class Pinit extends Activity{

Mise à jour:

findViewById (R.id. InpPH) passe à EditTex

Non, ce n'est pas le cas. Il se résout à une vue. Appeler .toString() sur une vue n'est pas ce que vous voulez. Vous voulez faire

((EditText) pinit.this.findViewById(R.id.InpPH)).getText().toString() 
+0

Hmm ... mais alors puisque findViewById (R.id.InpPH) .toString() se résout en une chaîne, a-t-il besoin de n'importe quel type cast? comme findViewById (R.id.InpPH) résout à EditText, et la méthode .tostring() renvoie une chaîne ... – bubu

+0

trop vrai ;-) aussi juste une petite suggestion findViewById est une tâche qui prend du temps, il fonctionne sur votre arbre de mise en page, faites-le une fois dans onCreate et gardez les vues comme membres alors quand vous venez nous les voir, ils sont déjà là – codeScriber

1

Je suppose que vous NumberFormatException à l'un des appels comme celui-ci:

Float.valueOf(findViewById(R.id.InpPH).toString()) 

Quant à la doc du NumberFormatException est obtenu si la chaîne est nulle, a une longueur de zéro ou ne peut pas être analysé en tant que valeur flottante. Cependant, vérifier la trace logCat pour cela donnerait une bonne réponse.

+0

Juste vu que vous avez mis à jour le poste avec trace LogCat. Ici c'est - 'NumberFormatException'. –

+0

C'est à vous de décider comment traiter 'NumberFormatException' ici - si vous devriez avoir une valeur float par défaut à restaurer si vous attrapez' NumberFormatException' ou si vous devez même casser l'action et afficher une boîte de dialogue d'alerte/toast. –

+2

Cette réponse est incorrecte. Le problème n'est pas que l'entrée n'est pas un nombre. C'est cet appel .toString() sur une classe View n'est pas ce que vous voulez. – Falmarri

Questions connexes