我想寫一個簡單的android應用程序來幫助解釋一些實驗室結果。如何在android中處理onClickListener?
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編程...所以我想這會是我犯的一些非常愚蠢的錯誤,但請大家幫忙。謝謝。
更新:將代碼更改爲此(減去邏輯部分,而我將在稍後完成) 但按下按鈕時仍然掛起。
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}
你需要從你的logcat發佈堆棧跟蹤,以便我們能夠給你任何幫助。 – Falmarri 2010-12-06 19:18:50
更新2: 原來,罪魁禍首不在點擊處理程序。這是類型轉換問題。事實證明,EditText的字符串值是通過.getText()而不是.toString()來請求的。我愚蠢的錯誤。 – bubu 2010-12-06 20:02:40