从数字EditText字段获取NumberFormatException

The*_*der 1 java android numberformatexception android-edittext

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

java.lang.NumberFormatException: invalid int "".
Run Code Online (Sandbox Code Playgroud)

网站developers.android.com说这是错误地将字符串转换为整数,但我不明白我是如何错误地从中获取数据的EditText.

这是我的代码......

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();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我的XML文件......

<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" /> 
Run Code Online (Sandbox Code Playgroud)

wat*_*ios 5

EditText最初的String值为空(即"").这显然不是一个数字,所以当你试图调用int i = Integer.parseInt(change);它时会给你一个错误说明.

有几种方法可以解决这个问题,但它基本上归结为要么通过设置初始值来防止错误,要么检测空字符串并正确处理它.

为了防止错误发生......

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);
Run Code Online (Sandbox Code Playgroud)

或设定初始值"0"EditText,但是这也显示值0EditText你的界面上而不是空的,所以它可能不适合所有目的...

<EditText android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:inputType="number"
          android:text="0"
          android:id="@+id/num_sensors" />
Run Code Online (Sandbox Code Playgroud)

如果你想检测错误并正确处理它,你会这样做......

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){}
Run Code Online (Sandbox Code Playgroud)

这基本上设置了值i = 0,并且只有在try{}代码没有给出错误时才会将其更改为不同的值.