Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 编辑文本到int崩溃_Java_Android_Xml - Fatal编程技术网

Java 编辑文本到int崩溃

Java 编辑文本到int崩溃,java,android,xml,Java,Android,Xml,所以基本上,我刚刚开始android开发,并根据youtube上的教程构建了一个应用程序。我决定离开一点,让它成为我想要的方式,现在我得到了一个错误 我试图做的是允许EditText决定此计数器应用程序的增量。这是我的密码: Java活动: counter = 0; reset = (Button) findViewById(R.id.reset); addone = (Button) findViewById(R.id.add);

所以基本上,我刚刚开始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:


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

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

android:text=“1”
添加到xml文件中的编辑文本中

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" >

您得到了空的EditText并试图从“”解析int,您将从Integer.parseInt(“”)方法得到NumberFormatException

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

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

发布堆栈跟踪,并向我们展示上面的代码是如何包含的(是否包含在方法、类范围等中)。好的,我已经这样做了,我知道这听起来可能很愚蠢,但实际上并没有增加数量。我是否需要添加一个按钮或一些它实际注册增量值的东西,而不是仅仅键入它?您不需要添加按钮
final int increment;
try {
    increment = Integer.parseInt();
} catch (NumberFormatException e) {
    // Do what you want
}