我从here得到了体重指数计算器计算器的代码,我试图在日食中运行给出的值,然后计算按钮不起作用。不幸的是,它已经关闭了它的错误。任何身体都可以帮助摆脱这种情况。
这是 logcat
12-17 03:09:53.130: D/dalvikvm(872): Not late-enabling CheckJNI (already on)
12-17 03:09:58.120: I/Choreographer(872): Skipped 32 frames! The application may be doing too much work on its main thread.
12-17 03:09:58.850: D/gralloc_goldfish(872): Emulator without GPU emulation detected.
12-17 03:10:59.470: I/Choreographer(872): Skipped 37 frames! The application may be doing too much work on its main thread.
12-17 03:11:31.400: I/Choreographer(872): Skipped 391 frames! The application may be doing too much work on its main thread.
12-17 03:11:38.210: I/Choreographer(872): Skipped 34 frames! The application may be doing too much work on its main thread.
12-17 03:11:38.450: I/Choreographer(872): Skipped 294 frames! The application may be doing too much work on its main thread.
12-17 03:11:38.680: I/Choreographer(872): Skipped 133 frames! The application may be doing too much work on its main thread.
12-17 03:11:39.870: I/Choreographer(872): Skipped 181 frames! The application may be doing too much work on its main thread.
12-17 03:11:40.300: I/Choreographer(872): Skipped 108 frames! The application may be doing too much work on its main thread.
12-17 03:11:40.440: I/Choreographer(872): Skipped 68 frames! The application may be doing too much work on its main thread.
12-17 03:11:40.920: I/Choreographer(872): Skipped 52 frames! The application may be doing too much work on its main thread.
12-17 03:11:42.270: I/Choreographer(872): Skipped 96 frames! The application may be doing too much work on its main thread.
12-17 03:11:44.000: D/dalvikvm(872): GC_FOR_ALLOC freed 126K, 7% free 3058K/3260K, paused 60ms, total 71ms
12-17 03:11:44.090: I/Choreographer(872): Skipped 53 frames! The application may be doing too much work on its main thread.
12-17 03:11:44.370: W/ViewRootImpl(872): Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_5, scanCode=6, metaState=0, flags=0x8, repeatCount=0, eventTime=283269, downTime=283269, deviceId=0, source=0x101 }
12-17 03:11:44.380: D/InputEventConsistencyVerifier(872): KeyEvent: ACTION_UP but key was not down.
12-17 03:11:44.380: D/InputEventConsistencyVerifier(872): in android.widget.PopupWindow$PopupViewContainer{b4d88ac0 V.E..... ......ID 0,0-99,85}
12-17 03:11:44.380: D/InputEventConsistencyVerifier(872): 0: sent at 283393000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_5, scanCode=6, metaState=0, flags=0x8, repeatCount=0, eventTime=283393, downTime=283269, deviceId=0, source=0x101 }
12-17 03:11:44.380: I/Choreographer(872): Skipped 375 frames! The application may be doing too much work on its main thread.
12-17 03:11:44.570: I/Choreographer(872): Skipped 120 frames! The application may be doing too much work on its main thread.
12-17 03:11:45.980: W/InputEventReceiver(872): Attempted to finish an input event but the input event receiver has already been disposed.
12-17 03:11:45.990: I/Choreographer(872): Skipped 501 frames! The application may be doing too much work on its main thread.
12-17 03:11:46.040: I/Choreographer(872): Skipped 54 frames! The application may be doing too much work on its main thread.
12-17 03:11:52.680: W/TextView(872): TextView does not support text selection. Action mode cancelled.
12-17 03:11:52.680: I/Choreographer(872): Skipped 66 frames! The application may be doing too much work on its main thread.
12-17 03:11:54.850: D/AndroidRuntime(872): Shutting down VM
12-17 03:11:54.860: W/dalvikvm(872): threadid=1: thread exiting with uncaught exception (group=0xb4a69b90)
12-17 03:11:54.910: E/AndroidRuntime(872): FATAL EXCEPTION: main
12-17 03:11:54.910: E/AndroidRuntime(872): Process: com.example.bmi, PID: 872
12-17 03:11:54.910: E/AndroidRuntime(872): java.lang.NumberFormatException: Invalid int: ""
12-17 03:11:54.910: E/AndroidRuntime(872): at
java.lang.Integer.invalidInt(Integer.java:137)
12-17 03:11:54.910: E/AndroidRuntime(872): at java.lang.Integer.parseInt(Integer.java:358)
12-17 03:11:54.910: E/AndroidRuntime(872): at java.lang.Integer.parseInt(Integer.java:331)
12-17 03:11:54.910: E/AndroidRuntime(872): at com.example.bmi.MainActivity$2.onClick(MainActivity.java:98)
12-17 03:11:54.910: E/AndroidRuntime(872): at android.view.View.performClick(View.java:4424)
12-17 03:11:54.910: E/AndroidRuntime(872): at android.view.View$PerformClick.run(View.java:18383)
12-17 03:11:54.910: E/AndroidRuntime(872): at android.os.Handler.handleCallback(Handler.java:733)
12-17 03:11:54.910: E/AndroidRuntime(872): at android.os.Handler.dispatchMessage(Handler.java:95)
12-17 03:11:54.910: E/AndroidRuntime(872): at android.os.Looper.loop(Looper.java:137)
12-17 03:11:54.910: E/AndroidRuntime(872): at android.app.ActivityThread.main(ActivityThread.java:4998)
12-17 03:11:54.910: E/AndroidRuntime(872): at java.lang.reflect.Method.invokeNative(Native Method)
12-17 03:11:54.910: E/AndroidRuntime(872): at java.lang.reflect.Method.invoke(Method.java:515)
12-17 03:11:54.910: E/AndroidRuntime(872): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-17 03:11:54.910: E/AndroidRuntime(872): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-17 03:11:54.910: E/AndroidRuntime(872): at dalvik.system.NativeStart.main(Native Method)
12-17 03:11:58.370: I/Process(872): Sending signal. PID: 872 SIG: 9
这是java
private Spinner heightspin;
private EditText feet;
private EditText cms;
private EditText lbs;
private TextView Lbstext;
private TextView result;
private RadioGroup rg;
private RadioButton radioSexButton;
public int hfeet;
public int hinc;
public int wlbs;
public int wkgs;
public int hcms;
public float bmifeet;
public float bmicms;
public String bmifeetStatus;
public String bmicmsstatus;
public int selectedId;
public String spintext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#c0c0c0")));
heightspin = (Spinner) findViewById(R.id.heightspin);
feet = (EditText) findViewById(R.id.feet);
cms = (EditText) findViewById(R.id.cms);
lbs = (EditText) findViewById(R.id.lbs);
Lbstext = (TextView) findViewById(R.id.Lbstext);
result = (TextView) findViewById(R.id.result);
rg = (RadioGroup) findViewById(R.id.radiosex);
spintext = heightspin.getSelectedItem().toString();
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.height_arrays, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
heightspin.setAdapter(adapter);
heightspin.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
feet.setVisibility(View.VISIBLE);
cms.setHint("Inches");
Lbstext.setText("Lbs");
lbs.setHint("Lbs");
} else {
feet.setVisibility(View.GONE);
cms.setHint("Cms");
Lbstext.setText("Kgs");
lbs.setHint("Kgs");
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
final Button button = (Button) findViewById(R.id.claculate);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (v.getId() == R.id.claculate) {
EditText feet = (EditText) findViewById(R.id.feet);
EditText inches = (EditText) findViewById(R.id.cms);
EditText lbs = (EditText) findViewById(R.id.lbs);
result = (TextView) findViewById(R.id.result);
hfeet = Integer.parseInt(feet.getText().toString());
hinc = Integer.parseInt(inches.getText().toString());
wlbs = Integer.parseInt(lbs.getText().toString());
selectedId = rg.getCheckedRadioButtonId();
radioSexButton = (RadioButton) findViewById(selectedId);
if (spintext.equalsIgnoreCase("Feet/Inches") && selectedId == R.id.radiomale) {
bmifeet = lbsbmi(hfeet, hinc, wlbs);
bmifeetStatus = staBMI(bmifeet);
result.setText("PRESENT STATUS :" + String.format("%.2f", bmifeet) + "-"
+ bmifeetStatus);
}
if (spintext.equalsIgnoreCase("Feet/Inches") && selectedId == R.id.radiofemale) {
bmifeet = lbsbmi(hfeet, hinc, wlbs);
bmicmsstatus = fstaBMI(bmifeet);
result.setText("PRESENT STATUS :" + String.format("%.2f", bmifeet) + "-"
+ bmicmsstatus);
}
if (spintext.equalsIgnoreCase("Cms") && selectedId == R.id.radiomale) {
bmicms = kgscbmi(hcms, wkgs);
bmifeetStatus = staBMI(bmicms);
result.setText("PRESENT STATUS :" + String.format("%.2f", bmicms) + "-"
+ bmifeetStatus);
}
if (spintext.equalsIgnoreCase("Cms") && selectedId == R.id.radiofemale) {
bmicms = kgscbmi(hinc, wlbs);
bmicmsstatus = fstaBMI(bmicms);
result.setText("PRESENT STATUS :" + String.format("%.2f", bmicms) + "-"
+ bmicmsstatus);
} else {
Toast.makeText(getApplicationContext(), "Please Put the values",
Toast.LENGTH_LONG).show();
}
}
}
});
}
private float lbsbmi(int feet, int inches, int lbs) {
int inc = feet + (inches / 12);
return (float) (lbs * 4.88 / (inc * inc));
}
private float kgscbmi(float cms, float kgs) {
float hgt = cms / 100;
return (float) (kgs / (hgt * hgt));
}
private String staBMI(float bmiValue) {
if (bmiValue < 18.5) {
return "Underweight";
} else if (bmiValue < 25) {
return "Normal";
} else if (bmiValue < 30) {
return "Overweight";
} else {
return "Obese";
}
}
private String fstaBMI(float bmiValue) {
if (bmiValue < 16.5) {
return "Underweight";
} else if (bmiValue < 22) {
return "Normal";
} else if (bmiValue < 27) {
return "Overweight";
} else {
return "Obese";
}
}
答案 0 :(得分:0)
问题:
EditText
中的一个(或全部)不是有效整数,并且Integer.parseInt(String)
失败。您应该处理任何NumberFormatException
并检查EditText
s(英尺,英寸,磅)的值。
来自您的logcat:
12-17 03:11:54.910:E / AndroidRuntime(872): java.lang.NumberFormatException:无效的int:“”
建议修复:
您可以将以下方法添加到Activity类中:
private int getIntFromEditText(EditText editText, int defaultValue) {
try {
return Integer.parseInt(editText.getText().toString().trim());
} catch (Exception e) {
return defaultValue;
}
}
然后更改
hfeet = Integer.parseInt(feet.getText().toString());
hinc = Integer.parseInt(inches.getText().toString());
wlbs = Integer.parseInt(lbs.getText().toString());
到
hfeet = getIntFromEditText(feet, 0);
hinc = getIntFromEditText(inches, 0);
wlbs = getIntFromEditText(lbs, 0);
答案 1 :(得分:0)
首先检查这些edittexts是否有空值(即""
)否则会抛出NumberFormatException
添加此
String hFeetStr = feet.getText().toString().trim();
String hincStr = inches.getText().toString().trim();
String wlbsStr = lbs.getText().toString().trim();
if(hFeetStr != null && !hFeetStr.equalsIgnoreCase(""))
hfeet = Integer.parseInt(hFeetStr);
else
hfeet = 0;
if(hincStr != null && !hincStr.equalsIgnoreCase(""))
hinc = Integer.parseInt(hincStr);
else
hinc = 0;
if(wlbsStr != null && !wlbsStr.equalsIgnoreCase(""))
wlbs = Integer.parseInt(wlbsStr);
else
wlbs = 0;
而不是
hfeet = Integer.parseInt(feet.getText().toString());
hinc = Integer.parseInt(inches.getText().toString());
wlbs = Integer.parseInt(lbs.getText().toString());