How to handle onClickListener in android?
我正在尝试编写一个简单的Android应用程序来帮助解释一些实验室结果。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 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); } } |
它确实编译并运行,但当我按下"解释"按钮时,它只是说应用程序......已意外停止。 请再试一次。 (强制关闭)。
我对android编程完全不熟悉......所以我想这会是我犯过的一些非常愚蠢的错误,但请帮忙。 谢谢。
更新:将代码更改为此(减去逻辑部分,我将在稍后完成)
按下按钮时仍然挂起。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 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的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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: android.widget.EditText@44f47e60 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} |
我看到的第一件事是你必须将它转换为它的视图类型
其次,你是在onclickListener中调用它。 所以findViewById()实际上甚至不应该编译。 你需要
1 | inpPH = Float.valueOf((TypeOfView) pinit.this.findViewById(R.id.InpPH).toString()).floatValue(); |
而且在java中,类应该从资本lettors开始,所以它应该是
更新:
findViewById(R.id.InpPH) resolves to
EditTex
不,不。 它解析为View。 在视图上调用.toString()不是您想要的。 你想做
1 | ((EditText) pinit.this.findViewById(R.id.InpPH)).getText().toString() |
我猜你在其中一个电话上得到
1 | Float.valueOf(findViewById(R.id.InpPH).toString()) |
至于doc,如果string为null,则得到
但是,检查logCat跟踪会得到一个干净的答案。