Получение NumberFormatException из числового поля EditText

Я пытаюсь создатьforпетля, чтобы добавить взгляды на макет.for-loop работает нормально (я пробовал с инициализированными переменными), но мне нужно получить целое число изEditText, Я использовалtry-catch и LogCat говорит мне следующее ...

java.lang.NumberFormatException: invalid int "".

Веб-сайт 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();
    }
  }
}

И это мой 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" /> 

Ответы на вопрос(7)

Ваш ответ на вопрос