-2

我正在尝试在 Android 4.3 中创建一个 BMI 应用程序。没有编译错误,但是当我开始在模拟器上运行它时,我收到消息 - “不幸的是,BMI 已停止。” 我不明白,我哪里出错了。有人可以帮我吗?我的java文件如下 -

package com.example.bmi;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

 public class MainActivity extends Activity {
   double bmi;
@Override
        protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    EditText e1=(EditText)findViewById(R.id.editText1);
    EditText e2=(EditText)findViewById(R.id.editText3);
    double d1 = Double.parseDouble(e1.getText().toString());
    double d2 = Double.parseDouble(e2.getText().toString());
    bmi = ( d2 / (d1*d1));

}
      public void showMessage(View v)
       {    

       Toast.makeText(getApplicationContext(), "Your BMI is " + bmi,                Toast.LENGTH_SHORT).show();

            }
        @Override
         public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
         }

                }

xml文件如下——

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="21dp"
    android:text="Height(m)"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="33dp"
    android:text="Weight(kg)"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="110dp"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView2"
    android:ems="10" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="75dp"
    android:text="@string/condition"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="98dp"
    android:text="Calculate" 
    android:onClick="showMessage"/>

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView1"
    android:layout_alignBottom="@+id/textView1"
    android:layout_marginLeft="47dp"
    android:layout_toRightOf="@+id/editText2"
    android:ems="10"
    android:inputType="number" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editText3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView2"
    android:layout_alignBottom="@+id/textView2"
    android:layout_alignLeft="@+id/editText1"
    android:ems="10"
    android:inputType="number" />

   </RelativeLayout>

这就是我完成申请的方式。

提前致谢。

4

5 回答 5

2

你的代码是错误的:

    double d1 = Double.parseDouble(e1.getText().toString());
    double d2 = Double.parseDouble(e2.getText().toString());

onCreate中,你什么也得不到。public void showMessage(View v)在 EditText 中输入内容后将它们移动到

于 2013-08-06T05:24:42.497 回答
2

you have number format exception as you are getting string from edit text without performing any action as the result of which it is crashing ..\ do it like that :

public class MainActivity extends Activity {
double bmi;
Button calculate ;
EditText e1,e2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    e1=(EditText)findViewById(R.id.editText1);
     e2=(EditText)findViewById(R.id.editText3);
 Button   calculate = (Button)findViewById(R.id.button1);
    calculate.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            double d1 = Double.parseDouble(e1.getText().toString());
            double d2 = Double.parseDouble(e2.getText().toString());
            bmi = ( d2 / (d1*d1));
            Toast.makeText(getApplicationContext(), "Your BMI is " + bmi,  Toast.LENGTH_SHORT).show();
        }
    });


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

于 2013-08-06T05:49:49.447 回答
0

此代码抛出异常:

double d1 = Double.parseDouble(e1.getText().toString());
double d2 = Double.parseDouble(e2.getText().toString());

bcs e1.getText().toString()== 空字符串。

将此代码移动到 btn 单击侦听器

于 2013-08-06T05:25:11.167 回答
0

更改您的 showMessage 方法,因为您无法在任何事件发生之前获取输入值。

public void showMessage(View v)
 {    

    double d1 = Double.parseDouble(e1.getText().toString());
    double d2 = Double.parseDouble(e2.getText().toString());
     bmi = ( d2 / (d1*d1));
   Toast.makeText(getApplicationContext(), "Your BMI is " + bmi,                Toast.LENGTH_SHORT).show();

}
于 2013-08-06T05:28:30.333 回答
0

Display integer or double value by toast this way..

if(v.getId()==R.id.Add_btn){
    double Add=(Val1+Val2);
    Text.setText("Addition is: "+Add);


    Toast toast = Toast.makeText(getApplicationContext(), "Add result is: "+ Add,Toast.LENGTH_SHORT);

    TextView toastMessage=(TextView) toast.getView().findViewById(android.R.id.message);
    toastMessage.setTextColor(Color.YELLOW);
    toast.show();
}
于 2019-10-12T16:43:58.103 回答