获得NumberFormatException异常从数字领域的EditText [英] Getting a NumberFormatException from a numerical EditText field

查看:189
本文介绍了获得NumberFormatException异常从数字领域的EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个 -loop添加视图布局。在 -loop工作正常(我和初始化变量尝试过),但我需要从一个整数的EditText 。我用了一个的try-catch 和LogCat中告诉我下面......

I'm trying to create a for-loop to add views to a layout. The for-loop is working fine (I tried it with initialized variables) but I need to get an integer from an EditText. I used a try-catch and LogCat tells me the following...

java.lang.NumberFormatException: invalid int "".

该网站developers.android.com说,这是从一个字符串错误地转换为整数,但我不明白我怎么可以得到不正确的的EditText

这是我的code ...

This is my code...

public class UserPref2Activity extends Activity 

{
@Override

public void onCreate(Bundle savedInstanceState) 
{
    try
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        EditText numSensors = (EditText) findViewById(R.id.num_sensors);
        String change = numSensors.getText().toString();
        int i = Integer.parseInt(change);

        int j; //iterates through the for loop 

        ScrollView sv = new ScrollView(this);
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        sv.addView(ll);

        for(j=1;j<i;j++)
        {
            EditText name = new EditText(this);
            name.setText("Name:");
            EditText type = new EditText(this);
            type.setText("Type:");
            EditText bits = new EditText(this);
            bits.setText("Bits:");
            ll.addView(name);
            ll.addView(type);
            ll.addView(bits);
        } 
        this.setContentView(sv);
    }
    catch (Exception e)
    {
        //sends actual error message to the log
        Log.e("ERROR", "ERROR IN CODE:" + e.toString());
        //prints out location of error
        e.printStackTrace();
    }
  }
}

这是我的XML文件...

And this is my XML file...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" 
android:id="@+id/userLayout" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Enter number of sensors:" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:id="@+id/num_sensors" /> 

推荐答案

的EditText 最初有一个空的字符串它的值(即)。这显然​​不是一个数字,所以当你试图调用 INT I =的Integer.parseInt(变化); 这是给你一个错误,说这样

Your EditText initially has an empty String for its value (ie ""). This is obviously not a number, so when you try to call int i = Integer.parseInt(change); it is giving you an error saying as such.

有几种方法来解决这个问题,但它基本上是由设置初始值,或检测一个空字符串,并妥善处理它归结为两种prevent错误。

There are a few ways to fix this, but it basically boils down to either prevent the error by setting an initial value, or detecting an empty string and handling it properly.

要prevent发生错误...

To prevent the error from occurring...

EditText numSensors = (EditText) findViewById(R.id.num_sensors);
String change = numSensors.getText().toString();
if (change.equals("")){ // detect an empty string and set it to "0" instead
    change = "0";
}
int i = Integer.parseInt(change);

或者设置的初始值0的EditText ,然而这也显示值 0 的EditText 你的界面而不是空的,所以它可能并不适合所有目的是合适的... <上/ p>

Or set the initial value as "0" for the EditText, however this also displays the value 0 in the EditText on your interface rather than being empty, so it might not be suitable for all purposes...

<EditText android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:inputType="number"
          android:text="0"
          android:id="@+id/num_sensors" />

如果你想检测到错误并妥善处理,你可以这样做......

If you want to detect the error and handle it properly, you would do this...

EditText numSensors = (EditText) findViewById(R.id.num_sensors);
String change = numSensors.getText().toString();
int i = 0; // set it to 0 as the default
try {
    i = Integer.parseInt(change);
}
catch (NumberFormatException e){}

这基本上设置 I = 0 的价值,只会将其更改为不同的值,如果尝试{} code不给出错误。

This basically sets the value of i = 0, and will only change it to a different value if the try{} code doesn't give an error.

这篇关于获得NumberFormatException异常从数字领域的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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