Issue
I am trying to write a simple android application to help interpret some lab results.
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); } }
It does compile and run but when I press on the "interpret" button it simply says The application ... has stopped unexpectedly. Please try again. (Force Close).
I am totally new to android programming... So I guess this would be some very stupid mistake I have made but please help. Thanks.
update: changed the code to this (minus the logic portion while i will complete later) but still hangs when press on the button.
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: 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}
Solution
First thing I see is that you have to cast this to the type of view that it is
inpPH = Float.valueOf(findViewById(R.id.InpPH).toString()).floatValue();
Second, you're calling this from within the onclickListener. So findViewById() really shouldn't even compile. You need
inpPH = Float.valueOf((TypeOfView) pinit.this.findViewById(R.id.InpPH).toString()).floatValue();
And also in java, classes should start with capital lettors, so it should be
public class Pinit extends Activity{
Update:
findViewById(R.id.InpPH) resolves to EditTex
No it doesn't. It resolves to a View. Calling .toString() on a view is not what you want. You want to do
((EditText) pinit.this.findViewById(R.id.InpPH)).getText().toString()
Answered By - Falmarri
Answer Checked By - Candace Johnson (JavaFixing Volunteer)