将EditText转换为float - android eclipse [英] convert EditText to float - android eclipse

查看:165
本文介绍了将EditText转换为float - android eclipse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 EditText 转换为float。我这样做:

I want to convert EditText to float. I do this:

EditText edt = (EditText) findViewById(R.id.price);
float number = Float.valueOf(edt.getText().toString());

应用程序未加载到我的设备和模拟器中。在他们两个写道:

the app not load in my device and the simulator too. in both of them write:

"the application has stopped unexpectedly. please try again."

如果我删除此行:

float number = Float.valueOf(edt.getText().toString());

该应用可以加载。

什么我能做什么?

推荐答案

我的猜测是 edt.getText()。toString()没有返回可以变成浮点数的值。尝试在代码周围添加以下内容

My guess would be edt.getText().toString() is not returning a value that can turned into a float. Try putting the following around your code

`

float number = 0;
try{
   number = Float.valueOf(edt.getText().toString());
} catch (NumberFormatException e) {
   e.printStackTrace();
}

`
这应该可以让你知道它为何会死。

` That should give you a clue why its dying.

这篇关于将EditText转换为float - android eclipse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆