您好我正在尝试开发一款计算器应用程序,它可以根据给定的重量和高度来计算您的瘦体重!一切正常,如果我把editText留空并按下计算按钮然后我的应用程序崩溃了!
这是我的代码
package com.example.bodybuildingcalc;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class LeanBodyMass extends Activity {
RadioGroup rg;
RadioButton male, female;
TextView tv;
int weight_, height_;
static double result;
EditText weight, height;
private static final int MALE = 100;
private static final int FEMALE = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lean_body_mass);
rg = (RadioGroup) findViewById(R.id.gender_lbm);
tv = (TextView) findViewById(R.id.result_lbm);
weight = (EditText) findViewById(R.id.weight_lbm);
height = (EditText) findViewById(R.id.height_lbm);
male = (RadioButton) findViewById(R.id.male_lbm);
male.setId(MALE);
female = (RadioButton) findViewById(R.id.female_lbm);
female.setId(FEMALE);
if (savedInstanceState != null) {
if (result != 0) {
tv.setVisibility(0);
if (result < 0) {
tv.setText("Your Lean Body Mass is Lesser than Zero!");
} else {
tv.setText("Your Lean Body Mass is " + (int) result + "kg");
}
}
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
outState.putDouble("RESULT", result);
}
public void Calculate(View v) {
switch (rg.getCheckedRadioButtonId()) {
case 100:
if (weight.getText().toString() != ""
& height.getText().toString() != ""){
weight_ = Integer.parseInt(weight.getText().toString());
height_ = Integer.parseInt(height.getText().toString());
result = (0.32810 * weight_) + (0.33929 * height_) - 29.5336;
if (result < 0) {
tv.setText("Your Lean Body Mass is Lesser than Zero!");
tv.setVisibility(0);
} else {
tv.setText("Your Lean Body Mass is " + (int) result + "kg");
tv.setVisibility(0);
}
} else {
Toast.makeText(getApplicationContext(),
"Oops! you forgot to enter height and weight",
Toast.LENGTH_SHORT).show();
}
break;
case 101:
weight_ = Integer.parseInt(weight.getText().toString());
height_ = Integer.parseInt(height.getText().toString());
result = (0.29569 * weight_) + (0.41813 * height_) - 43.2933;
if (result < 0) {
tv.setText("Your Lean Body Mass is Lesser than Zero!");
tv.setVisibility(0);
} else {
tv.setText("Your Lean Body Mass is " + (int) result + "kg");
tv.setVisibility(0);
}
break;
case -1:
// first
if (weight.getText().toString().matches("")) {
// secound
if (height.getText().toString().matches("")) {
Toast.makeText(getApplicationContext(),
"Oops! you forgot to enter height and weight",
Toast.LENGTH_SHORT).show();
// secound-end
} else {
// three
Toast.makeText(getApplicationContext(),
"Oops! you forgot to enter weight",
Toast.LENGTH_SHORT).show();
// secound-end
}
} else {
if (height.getText().toString().matches("")) {
Toast.makeText(getApplicationContext(),
"Oops! you forgot to enter height!",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(),
"Please Select Your Gender!", Toast.LENGTH_SHORT)
.show();
}
}
}
}}
这是Logcat:
04-18 00:27:53.206: D/AndroidRuntime(990): Shutting down VM
04-18 00:27:53.216: W/dalvikvm(990): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-18 00:27:53.256: E/AndroidRuntime(990): FATAL EXCEPTION: main
04-18 00:27:53.256: E/AndroidRuntime(990): java.lang.IllegalStateException: Could not execute method of the activity
04-18 00:27:53.256: E/AndroidRuntime(990): at android.view.View$1.onClick(View.java:2072)
04-18 00:27:53.256: E/AndroidRuntime(990): at android.view.View.performClick(View.java:2408)
04-18 00:27:53.256: E/AndroidRuntime(990): at android.view.View$PerformClick.run(View.java:8816)
04-18 00:27:53.256: E/AndroidRuntime(990): at android.os.Handler.handleCallback(Handler.java:587)
04-18 00:27:53.256: E/AndroidRuntime(990): at android.os.Handler.dispatchMessage(Handler.java:92)
04-18 00:27:53.256: E/AndroidRuntime(990): at android.os.Looper.loop(Looper.java:123)
04-18 00:27:53.256: E/AndroidRuntime(990): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-18 00:27:53.256: E/AndroidRuntime(990): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 00:27:53.256: E/AndroidRuntime(990): at java.lang.reflect.Method.invoke(Method.java:521)
04-18 00:27:53.256: E/AndroidRuntime(990): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-18 00:27:53.256: E/AndroidRuntime(990): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-18 00:27:53.256: E/AndroidRuntime(990): at dalvik.system.NativeStart.main(Native Method)
04-18 00:27:53.256: E/AndroidRuntime(990): Caused by: java.lang.reflect.InvocationTargetException
04-18 00:27:53.256: E/AndroidRuntime(990): at com.example.bodybuildingcalc.LeanBodyMass.Calculate(LeanBodyMass.java:64)
04-18 00:27:53.256: E/AndroidRuntime(990): at com.example.bodybuildingcalc.LeanBodyMass.Calculate(LeanBodyMass.java:64)
04-18 00:27:53.256: E/AndroidRuntime(990): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 00:27:53.256: E/AndroidRuntime(990): at java.lang.reflect.Method.invoke(Method.java:521)
04-18 00:27:53.256: E/AndroidRuntime(990): at android.view.View$1.onClick(View.java:2067)
04-18 00:27:53.256: E/AndroidRuntime(990): ... 11 more
04-18 00:27:53.256: E/AndroidRuntime(990): Caused by: java.lang.NumberFormatException: unable to parse '' as integer
04-18 00:27:53.256: E/AndroidRuntime(990): Caused by: java.lang.NumberFormatException: unable to parse '' as integer
04-18 00:27:53.256: E/AndroidRuntime(990): at java.lang.Integer.parseInt(Integer.java:412)
04-18 00:27:53.256: E/AndroidRuntime(990): at java.lang.Integer.parseInt(Integer.java:382)
04-18 00:27:53.256: E/AndroidRuntime(990): ... 15 more
这是布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Weight :"
/>
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/weight_lbm"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Height :"
/>
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/height_lbm"/>
</LinearLayout>
<RadioGroup
android:id="@+id/gender_lbm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal"
android:padding="5dp" >
<RadioButton
android:text="Male"
android:id="@+id/male_lbm"
/>
<RadioButton
android:text="Female"
android:id="@+id/female_lbm"
/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Calculate"
android:onClick="Calculate"
/>
</RadioGroup>
<TextView
android:id="@+id/result_lbm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:visibility="invisible" />
</LinearLayout>
答案 0 :(得分:0)
错误java.lang.NumberFormatException: unable to parse '' as integer
表示您尝试转换为整数的数字不是数字或不存在。
如果您的EditText
null ,则无法将null值转换为整数。
因此,请务必检查EditText
的空值,如下所示:
public void Calculate(View v) {
switch (rg.getCheckedRadioButtonId()) {
case 100:
if (!TextUtils.isEmpty(weight.getText().toString())
& !TextUtils.isEmpty(height.getText().toString())){
weight_ = Integer.parseInt(weight.getText().toString());
height_ = Integer.parseInt(height.getText().toString());
result = (0.32810 * weight_) + (0.33929 * height_) - 29.5336;
if (result < 0) {
tv.setText("Your Lean Body Mass is Lesser than Zero!");
tv.setVisibility(0);
} else {
tv.setText("Your Lean Body Mass is " + (int) result + "kg");
tv.setVisibility(0);
}
答案 1 :(得分:0)
使用TextUtils.isEmpty(edittext.getText()。toString())来验证edittext。如果edittext为空,则向用户显示警告吐司。
答案 2 :(得分:0)
当您的edittext为空时,在运行时Integer.parseInt(&#34;&#34;); parseInt方法无法解析任何其他字符串。 您可以使用parseInt方法解析的几个示例是&#34; 8&#34;,&#34; 0&#34;,&#34; 100&#34;等。 &#34; 8s&#34;,&#34; askls&#34;等等这些字符串无法使用parseInt方法解析