java If语句强制程序崩溃
我以前问过几个问题,但我决定回到基本问题上来
我正在创建一个数学安卓应用程序,每个页面有三个TextField
,用户可以在其中输入一个数字,我已经将这些TextField
解析为一个双精度。但是,假设用户在2个TextField
中输入一个数字,我希望程序告诉我第三个文本字段的值
目前我可以让程序运行一次计算,但是当我为剩下的两个TextField
添加if语句时,当我单击calculate时程序崩溃。
程序背后的关键是发现哪个TextField
是空的,这样程序就可以进行正确的计算
我目前正在尝试.getText().toString().isEmpty()
,但这不起作用,我也尝试过。length()==0,但这也不起作用
当我运行emulator并单击calculator按钮时,当我在中添加更多变量时,程序崩溃,但是只需一次计算就可以了
有人能帮我找出If语句不起作用的原因吗?如果可能,有人能提供解决方案吗
很抱歉,如果我的格式设置被关闭,我对Java比较陌生,下面是Java
Button calc1 = (Button) findViewById(R.id.current_calculate);
calc1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText Charge1 = (EditText) findViewById(R.id.number_input_2);
EditText Time1 = (EditText) findViewById(R.id.number_input_3);
EditText current1 = (EditText) findViewById(R.id.number_input_1);
TextView Distances_answer = (TextView) findViewById(R.id.Distances_answer);
double charge = Double
.parseDouble(Charge1.getText().toString());
double time = Double.parseDouble(Time1.getText().toString());
double current = Double.parseDouble(current1.getText()
.toString());
// Time is a class in Java
if (current1.getText().toString().isEmpty()) {
Distances_answer.setText("" + charge * time);
} else if (Time1.length() == 0) {
Distances_answer.setText(" " + charge / current);
}
}
});
我还提供了XML以备需要
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:id="@+id/RelativeLayout1"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:background="@drawable/background1"
安卓:gravity="fill"
安卓:orientation="vertical"
安卓:textColor="#ffffff"
tools:context=".CurrentPage" >
<TextView
安卓:id="@+id/Current_hint"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_margin="20dp"
安卓:layout_marginTop="33dp"
安卓:text="Welcome to the Current Calculation page.
Hint - Use the check boxes to find the values that are missing "
安卓:textAppearance="?安卓:attr/textAppearanceMedium"
安卓:textColor="#ffffff"
tools:ignore="HardcodedText" />
<TextView
安卓:id="@+id/Equation_letter"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/current_calculate"
安卓:layout_marginTop="37dp"
安卓:layout_toLeftOf="@+id/current_calculate"
安卓:text=" HELLO "
安卓:textAppearance="?安卓:attr/textAppearanceLarge"
安卓:textColor="#ffffff"
tools:ignore="HardcodedText" />
<TextView
安卓:id="@+id/Distances_answer"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignBaseline="@+id/Equation_letter"
安卓:layout_alignBottom="@+id/Equation_letter"
安卓:layout_alignLeft="@+id/current_calculate"
安卓:layout_alignParentRight="true"
安卓:textAppearance="?安卓:attr/textAppearanceLarge"
安卓:textColor="#ffffff" />
<TextView
安卓:id="@+id/t"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignLeft="@+id/Q"
安卓:layout_alignRight="@+id/Q"
安卓:layout_below="@+id/Q"
安卓:layout_marginTop="36dp"
安卓:gravity="right|top"
安卓:text="t ="
安卓:textAppearance="?安卓:attr/textAppearanceLarge"
安卓:textColor="#ffffff"
tools:ignore="HardcodedText" />
<CheckBox
安卓:id="@+id/custom3"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_above="@+id/t"
安卓:layout_alignLeft="@+id/custom2"
安卓:background="@drawable/check_box_new"
安卓:button="@drawable/check_box_new" />
<CheckBox
安卓:id="@+id/custom1"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignBottom="@+id/number_input_3"
安卓:layout_alignLeft="@+id/custom3"
安卓:background="@drawable/check_box_new"
安卓:button="@drawable/check_box_new" />
<ScrollView
安卓:id="@+id/scrollView1"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignLeft="@+id/Current_hint"
安卓:layout_alignRight="@+id/Current_hint"
安卓:layout_below="@+id/Equation_letter"
安卓:layout_marginTop="25dp" >
<LinearLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:orientation="vertical" >
<TextView
安卓:id="@+id/CurrentmainHelp"
安卓:layout_width="wrap_content"
安卓:layout_height="0dp"
安卓:layout_weight="1.31"
安卓:text="HELP! This equation can be used to calculate the current of an object in amps. The correct equation to discover amps is I=Q/t. Where I = current, Q = charge flowing past a point in the circuit, and t = time taken for the charge to flow. Please note, Q is measure in coulombs and t is measured in seconds, with I being measured in amperes.
Still need more help? "
安卓:textAppearance="?安卓:attr/textAppearanceMedium"
安卓:textSize="18sp"
tools:ignore="HardcodedText" />
<Button
安卓:id="@+id/Yes_Please"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_gravity="center"
安卓:layout_marginBottom="36dp"
安卓:layout_weight="1.31"
安卓:onClick="CurrentHelp"
安卓:text="Yes Please"
安卓:textColor="#ffffff" />
</LinearLayout>
</ScrollView>
<Button
安卓:id="@+id/current_calculate"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_centerHorizontal="true"
安卓:layout_centerVertical="true"
安卓:text="@string/Calculate_Current"
安卓:textColor="#ffffff" />
<EditText
安卓:id="@+id/number_input_2"
安卓:layout_width="100dp"
安卓:layout_height="wrap_content"
安卓:layout_above="@+id/t"
安卓:layout_alignLeft="@+id/number_input_1"
安卓:layout_alignRight="@+id/number_input_1"
安卓:ems="10"
安卓:focusable="true"
安卓:inputType="numberDecimal"
安卓:textColor="#ffffff" >
<requestFocus />
</EditText>
<EditText
安卓:id="@+id/number_input_3"
安卓:layout_width="100dp"
安卓:layout_height="wrap_content"
安卓:layout_alignBottom="@+id/t"
安卓:layout_alignLeft="@+id/number_input_2"
安卓:layout_alignRight="@+id/number_input_2"
安卓:ems="10"
安卓:focusable="true"
安卓:inputType="numberDecimal"
安卓:textColor="#ffffff" />
<CheckBox
安卓:id="@+id/custom2"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignBottom="@+id/number_input_1"
安卓:layout_marginLeft="45dp"
安卓:layout_toRightOf="@+id/number_input_1"
安卓:background="@drawable/check_box_new"
安卓:button="@drawable/check_box_new" />
<EditText
安卓:id="@+id/number_input_1"
安卓:layout_width="150dp"
安卓:layout_height="wrap_content"
安卓:layout_above="@+id/Q"
安卓:layout_alignRight="@+id/current_calculate"
安卓:ems="10"
安卓:focusable="true"
安卓:inputType="numberDecimal"
安卓:singleLine="true"
安卓:textColor="#ffffff" />
<TextView
安卓:id="@+id/Q"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignLeft="@+id/Equation_letter"
安卓:layout_below="@+id/I"
安卓:layout_marginTop="36dp"
安卓:gravity="right|top"
安卓:text="Q ="
安卓:textAppearance="?安卓:attr/textAppearanceLarge"
安卓:textColor="#ffffff"
tools:ignore="HardcodedText" />
<TextView
安卓:id="@+id/I"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignLeft="@+id/Q"
安卓:layout_alignRight="@+id/Q"
安卓:layout_below="@+id/Current_hint"
安卓:layout_marginTop="65dp"
安卓:gravity="right|top"
安卓:text="I ="
安卓:textAppearance="?安卓:attr/textAppearanceLarge"
安卓:textColor="#ffffff"
tools:ignore="HardcodedText" />
# 1 楼答案
我相信应用程序崩溃是因为您正在解析
EditText
到double
的内容,即使它们可能是空的。如果Double.parseDouble
函数无法解析给定String
中的double
,它将抛出一个NumberFormatException
。要解决此问题,请在尝试解析或捕获异常之前执行空字符串检查例如: