简体   繁体   English

空的编辑文本给我0值错误

[英]empty Edit Text gives me error for 0 value

I'm developing a project and I have problem with my addition for empty EditText( unused) . 我正在开发一个项目,我为空的EditText(未使用)添加了一些问题。 If I enter a number in my editText then click the button it will show the output in the viewtext (which is right) also if you keep doing that the number will be added to the total number . 如果我在editText中输入一个数字,然后单击按钮,它将在视图文本中显示输出(是正确的),如果您继续这样做,该数字将被添加到总数中。

BUT !! 但是! if I click the button without enter any number in my edittext . 如果我单击按钮时未在edittext中输入任何数字。 the project will be crashed showing the project has stopped ! 该项目将崩溃,表明该项目已停止!

What I want is : if I just click the button without enter any number the 0 will be added to the total number . 我想要的是:如果我仅单击按钮而不输入任何数字,则0将被添加到总数中。 ex.. 5 >> Button >> 5+0=5 show 5 for total >> 6 >> Button >> 6 + 5= 11 >> 0 >> Button >> 0 + 11 = 11 (<< KEY) 例如5 >>按钮>> 5 + 0 = 5总共显示5 >> 6 >>按钮>> 6 + 5 = 11 >> 0 >>按钮>> 0 + 11 = 11(<<键)

My code 我的密码

MyActivity.java

    package b3du.im.myapplication;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class MyActivity extends ActionBarActivity {


    int total = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
    }

    public void Add(View v){

        TextView text1 =  (TextView) findViewById(R.id.text1);
        EditText  text2 =  (EditText) findViewById(R.id.text2);


        int valueText2 = Integer.parseInt(text2.getText().toString());

        total= Integer.parseInt(text1.getText().toString());

        total += valueText2;

        text1.setText(Integer.toString(total));

        text2.setText("");

    }

}

activity_my.xml activity_my.xml

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center"
   >

    <TextView
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:inputType="phone"
        android:ems="10"
        android:id="@+id/text1"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:text="0"
        android:textSize="30dp"
        android:gravity="center" />

    <EditText
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/text2"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:hint="0"
        android:gravity="center" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add"
        android:id="@+id/button"
        android:layout_gravity="center_horizontal"
        android:onClick="Add" />
</LinearLayout>

If you enter nothing you get a blank in text2.getText().toString() and you get a NumberFormatException. 如果您不输入任何内容,则text2.getText().toString()text2.getText().toString()空,并且您将获得NumberFormatException。

You should set `valueText2 to 0 if no text is entered: 如果未输入任何文本,则应将`valueText2设置为0:

int valueText2 = 0;

if (text2.getText() != null && !text2.getText().toString().equals("")){
   int valueText2 = Integer.parseInt(text2.getText().toString());
int valueText2 = Integer.parseInt(text2.getText().toString());

If the string is empty, parseInt will throw an exception. 如果字符串为空,则parseInt将引发异常。 You need to check if its empty first and not vall parseInt. 您需要先检查其是否为空,而不要检查其parseInt。

Also, in the future always post the stack trace from logcat for a crash. 另外,将来始终请从logcat发布堆栈跟踪信息以免发生崩溃。 It tells you exactly what the error is. 它确切地告诉您错误是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查编辑文本中的空值 - check empty value in edit text “ this”给我片段错误 - “this” gives me an error in fragment Java - 使用扫描仪读取文本文件并存储它,给我空的 ArrayList。 使用分隔符拆分单词 - Java - Read text file and store it using scanner, gives me empty ArrayList. Split words by using Delimeter SAXParser给我62个元素之后的第二个标签提供了一个空值,原因不明,为什么? - SAXParser gives me an empty value for the second tag after 62 element for no clear reason, why? 如何创建一个包含可选的哈希图,并在检索它时给我值或 Optional.empty 但有限制 - How to create a hashmap that contains optional and when retreiving it gives me the value or Optional.empty but there are restrictions .getDownloadUrl() 给我错误 - Android - .getDownloadUrl() gives me error - Android Android初学者:空编辑文本 - Android Beginner: Empty Edit Text ObjectInputStream给了我一个空对象(但是readed文件不为空) - ObjectInputStream gives me an empty object (but the readed file is not empty) 我想从 Activity 获取编辑文本数据并显示在 Fragment 中,但它给了我错误 - I want to get edit text data from Activity and and show in Fragment but it give me error 将 XML 作为 CLOB 传递给 Oracle 会出现“ORA-22922:不存在的 LOB 值”错误 - Passing XML as a CLOB to Oracle gives me "ORA-22922: nonexistent LOB value" error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM