使用 EditText 的 Android 数字格式异常
我正在运行一个简单的流程,
只需用户在 EditText 中输入一个数字,选择单选按钮,然后按计算即可。单选按钮连接到 if 语句,而 if 语句又直接指向返回的浮点数,替换 EditText 中显示的数字。然而,如果在输入任何数字之前按下计算按钮,则会出现数字格式异常,导致应用程序崩溃。
现在,我将在 EditText 中输入一个默认值,以减少发生这种情况的可能性,但我想知道是否有一种方法可以使用 if 语句或类似的东西来避免异常,
是一些示例代码
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import com.admob.android.ads.AdManager;
import com.admob.android.ads.AdView;
import com.medialets.android.analytics.MMAnalyticsManager;
public class Area extends Activity {
private EditText text9;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.area);
text9 = (EditText)findViewById(R.id.EditText09);
public void myClickHandler09(View view){
switch (view.getId()) {
case R.id.Button09:
RadioButton SeveralRadioButtons = (RadioButton) findViewById (R.id.RadioButton901);
float inputValue = Float.parseFloat(text9.getText().toString());
String checkValue = String.valueOf(inputValue);
if (checkValue.equals("")){
text9.setText(String.valueOf(""));
} else {
if (SeveralRadioButtons.isChecked()) {
text9.setText(String
.valueOf(conversionfactor(inputValue)));
}
break;
}}}
private double conversionfactor(float f){
return f * 6.4516;
}
这里 (几个单选按钮) 被称为实际上有几个单选按钮以及每个单选按钮和一个 if 语句以及 private double
如您所见,我已经尝试解决该问题,但数字格式异常仍然出现。
I have a simple process running
Put simply the user inputs a number into an EditText, picks Radio Button and then presses calculate. The radio buttons are connected to if statements that in turn direct towards floats that are returned replacing the number shown in the EditText. However it appears that if the calculate button is pressed before any numbers are entered then a number format exception occours causing the app to crash.
For now I will enter a default value into the EditText to decrease the chances of this happening, but i was wondering if there was a way to avoid an exception all together using something like an if statement or something similar
here is some example code
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import com.admob.android.ads.AdManager;
import com.admob.android.ads.AdView;
import com.medialets.android.analytics.MMAnalyticsManager;
public class Area extends Activity {
private EditText text9;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.area);
text9 = (EditText)findViewById(R.id.EditText09);
public void myClickHandler09(View view){
switch (view.getId()) {
case R.id.Button09:
RadioButton SeveralRadioButtons = (RadioButton) findViewById (R.id.RadioButton901);
float inputValue = Float.parseFloat(text9.getText().toString());
String checkValue = String.valueOf(inputValue);
if (checkValue.equals("")){
text9.setText(String.valueOf(""));
} else {
if (SeveralRadioButtons.isChecked()) {
text9.setText(String
.valueOf(conversionfactor(inputValue)));
}
break;
}}}
private double conversionfactor(float f){
return f * 6.4516;
}
Where
(SeveralRadioButtons)
is called there are in fact several radio buttons and an if statement and private double for each one respectively
As you can see I have already made an attempt at fixing the problem but the number format exception still appears.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除了遵循检查
null
和""
值的其他建议之外,您还可以捕获NumberFormatException
并处理错误情况。您可以中止计算并可能显示错误消息。处理抛出的异常意味着如果您错过了一些其他边缘情况,您的用户将不会遇到完整的应用程序崩溃。
As well as following the other suggestions for checking for
null
and""
values, you can also catch theNumberFormatException
and handle the error case. You could abort the calculation and perhaps show an error message.Handling the exception if it is thrown means that if there is some other edge case that you have missed, your users won't experience a full application crash.
在转换周围放置一条 if 语句,检查是否为 null(首先!)或空字符串。默认值一直有效,直到人们在文本框中输入空字符串为止。
put an if statement around your conversion checking for null (first!) or an empty string. A default value works until people put an empty string in the text box.
在按钮的 onClickListener 中,您可以编写一个 if 语句,在实际将数字发送到您要发送的任何位置之前检查字符串是否为 null 或“”...
In the onClickListener of your button you can write an if statement that checks if the string is null or "" before actually sending the number to wherever you are sending it...