0

こんにちは、指定された体重と身長から除脂肪体重を計算できる電卓アプリを開発しようとしています! 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>
4

3 に答える 3