使用 EditText 的 Android 数字格式异常

发布于 2024-10-19 09:31:21 字数 1586 浏览 2 评论 0原文

我正在运行一个简单的流程,

只需用户在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑脸一如从前 2024-10-26 09:31:21

除了遵循检查 null"" 值的其他建议之外,您还可以捕获 NumberFormatException 并处理错误情况。您可以中止计算并可能显示错误消息。

处理抛出的异常意味着如果您错过了一些其他边缘情况,您的用户将不会遇到完整的应用程序崩溃。

As well as following the other suggestions for checking for null and "" values, you can also catch the NumberFormatException 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.

孤千羽 2024-10-26 09:31:21

在转换周围放置一条 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.

太阳哥哥 2024-10-26 09:31:21

在按钮的 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文