我在我的EditText字段中输入浮动数字,并希望根据下面的一些逻辑计算结果,并在TextView中显示结果。我想知道这个方法有什么问题,因为我收到这个错误特别的方法。我知道它很简单但我是Android初学者。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi);
bmi1=(EditText) findViewById(R.id.editText1);
bmi2=(EditText) findViewById(R.id.editText2);
bmi3=(TextView) findViewById(R.id.bmiText);
bmi2.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable ab) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){
calRes();
}
});
ImageButton ImBtn=(ImageButton)findViewById(R.id.imageButton1);
ImBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent newIntent=new Intent(Activity1.this,DragNDropListActivity.class);
startActivity(newIntent);
finish();
}
});
}
public void calRes() {
String a;
String b;
float c;
a = bmi1.getEditableText().toString(); //gets you the contents of edit text
b = bmi2.getEditableText().toString();
c= Float.parseFloat(a)/(Float.parseFloat(b)*Float.parseFloat(b));
bmi3.setText(String.valueOf(c));
}
}
08-29 12:05:03.658: ERROR/AndroidRuntime(299): FATAL EXCEPTION: main
08-29 12:05:03.658: ERROR/AndroidRuntime(299): java.lang.NumberFormatException: lengte in metre
08-29 12:05:03.658: ERROR/AndroidRuntime(299): at org.apache.harmony.luni.util.FloatingPointParser.initialParse(FloatingPointParser.java:130)
08-29 12:05:03.658: ERROR/AndroidRuntime(299): at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:319)
08-29 12:05:03.658: ERROR/AndroidRuntime(299): at java.lang.Float.parseFloat(Float.java:291)
08-29 12:05:03.658: ERROR/AndroidRuntime(299): at com.android.mds.health.nursing.formula.Activity1.calRes(Activity1.java:87)
08-29 12:05:03.658: ERROR/AndroidRuntime(299): at com.android.mds.health.nursing.formula.Activity1$1.onTextChanged(Activity1.java:45)
08-29 12:05:03.658: ERROR/AndroidRuntime(299): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
08-29 12:05:03.658: ERROR/AndroidRuntime(299): at android.widget.TextView.handleTextChanged(TextView.java:6172)
08-29 12:05:03.658: ERROR/AndroidRuntime(299): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:6316)
08-29 12:05:03.658: ERROR/AndroidRuntime(299): at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:889)
08-29 12:05:03.658: ERROR/AndroidRuntime(299): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:400)
08-29 12:05:03.658: ERROR/AndroidRuntime(299): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
08-29 12:05:03.658: ERROR/AndroidRuntime(299): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
08-29 12:05:03.658: ERROR/AndroidRuntime(299): at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:218)
08-29 12:05:03.658: ERROR/AndroidRuntime(299): at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:28)
08-29 12:05:03.658: ERROR/AndroidRuntime(299): at android.text.method.BaseKeyListener.backspace(BaseKeyListener.java:60)
08-29 12:05:03.658: ERROR/AndroidRuntime(299): at android.text.method.BaseKeyListener.onKeyDown(BaseKeyListener.java:123)
08-29 12:05:03.658: ERROR/AndroidRuntime(299): at android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:327)
08-29 12:05:03.658: ERROR/AndroidRuntime(299): at android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:132)
答案 0 :(得分:1)
而不是这个
bmi3.setText(String.valueOf(c));
这样做
bmi3.setText(""+c);
编辑:使用valueOf
代替parseFloat
c= Float.valueOf(a)/(Float.valueOf(b)*Float.valueOf(b));
编辑2:
将所有值输入edittexts
时计算BMI。按一下按钮,然后在按钮的calRes
中拨打onClick
。您将获得异常,因为当您仍然进入第一个字段并且其余字段为空时,会调用calRes。空字段导致崩溃,因为它们中没有任何值可以转换。
答案 1 :(得分:1)
好的,看了你的代码之后,你在解析字符串浮动时遇到了问题,
如果你对字符串a和b的值没有问题,那么它应该可以正常工作。
看,我试过这个完美的工作,
String a,b;
Float c;
tv =(TextView)findViewById(R.id.tv_test);
a = "100"; //gets you the contents of EditText
b = "10";
c= Float.parseFloat(a)/(Float.parseFloat(b)*Float.parseFloat(b));
tv.setText(String.valueOf(c));
Edit: Also take care of your editText input its have only numeric values in string
format, not any characters or special symbols.
答案 2 :(得分:0)
也许不是这样,
a = bmi1.getText().toString(); //gets you the contents of EditText
b = bmi2.getText().toString();
你应该试试这个,
a = bmi1.getEditableText().toString(); //gets you the contents of EditText
b = bmi2.getEditableText().toString();
答案 3 :(得分:0)
如果您的EditText
(bmi1& bmi2,a,b)中有任何字母或特殊字符,则必须出现此错误,请使用System.out.println
检查EditText的值... becuase float Variablt必须是数字和点(。)
答案 4 :(得分:0)
从查看您的初始错误看起来好像您正在尝试从字符串'lengte in meter'解析一个浮点数。这是您布局中标签的文字吗?肯定不是一个可解析的数字:) 也许如果您仍然遇到问题,可以发布xml布局文件。
安东尼