[英]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.