Edittext to int崩溃

时间:2013-02-17 00:48:20

标签: java android xml

所以基本上,我刚开始进行android开发,并且正在使用youtube上的教程构建应用程序。我决定稍微离开并按照我想要的方式进行,现在我收到了错误。

我要做的是允许EditText决定此计数器应用程序的增量。这是我的代码:

Java活动:

        counter = 0;
        reset = (Button) findViewById(R.id.reset);
        addone = (Button) findViewById(R.id.add);
        takeOne = (Button) findViewById(R.id.take);
        tvCounter = (TextView) findViewById(R.id.fullscreen_content);
        incrementer = (EditText) findViewById(R.id.number);

        final int increment = Integer.parseInt(incrementer.getText().toString());

        addone.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                counter += increment;
                tvCounter.setText("" + counter);
            }
        });

        takeOne.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                counter -= increment;
                tvCounter.setText("" + counter);
            }
        });

        reset.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                counter = 0;
                tvCounter.setText("" + counter);
            }

XML:

       <Button
            android:id="@+id/add"
            style="?buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Add One" />
        <Button
            android:id="@+id/take"
            style="?buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Take One" />

        <Button
            android:id="@+id/reset"
            style="?buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Reset" />
    </LinearLayout>

    <EditText
        android:id="@+id/number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:ems="10"
        android:inputType="number" >

        <requestFocus />
    </EditText>

3 个答案:

答案 0 :(得分:1)

02-17 09:35:56.313: E/AndroidRuntime(3177): Caused by: java.lang.NumberFormatException: Invalid int: ""
02-17 09:35:56.313: E/AndroidRuntime(3177):     at java.lang.Integer.invalidInt(Integer.java:138)
02-17 09:35:56.313: E/AndroidRuntime(3177):     at java.lang.Integer.parseInt(Integer.java:359)
02-17 09:35:56.313: E/AndroidRuntime(3177):     at java.lang.Integer.parseInt(Integer.java:332)
02-17 09:35:56.313: E/AndroidRuntime(3177):     at 

这可能是错误并在此行上发现

final int increment = Integer.parseInt(incrementer.getText().toString());

最好为您的EditText

添加默认值
 <EditText
        android:id="@+id/number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:ems="10"
        android:text="0" 
        android:inputType="number" >

答案 1 :(得分:0)

它可能会崩溃,因为EditText的内容不是数字。 假设这是你的onCreate方法,此时你不需要阅读edittext。 在更新计数器之前,在按钮的onClick方法中解析edittext的内容。

此外,由于您不知道用户输入的文本类型,您应该使用try和catch来包围parseInt调用,以防止应用程序在EditText不包含数字时崩溃。

android:text="1"添加到xml文件中的EditText。

答案 2 :(得分:0)

你有一个空的EditText并尝试从“”解析,你将从Integer.parseInt(“”)方法得到NumberFormatException。

只需使用try / catch来捕获和处理异常。

final int increment;
try {
    increment = Integer.parseInt();
} catch (NumberFormatException e) {
    // Do what you want
}