之前我曾问过几个问题,但我决定回到基础。
我正在创建一个maths android应用程序,每个页面有三个TextField
s,用户可以输入一个数字,我已经将这些TextField
解析为一个double。但是,假设用户在TextField
中输入了一个数字,我希望程序告诉我第三个文本字段的值。
目前我可以让程序运行一个计算,但是当我为剩下的两个TextField
添加if语句时,程序在我点击计算时崩溃了。
该程序背后的关键是发现哪个TextField
为空,以便程序可以进行正确的计算。
我目前正在尝试.getText().toString().isEmpty()
,但这不起作用,我也尝试过.length()== 0,但这也不起作用。
当我运行模拟器并单击计算器按钮时,程序会在我添加更多变量时崩溃,但只需一次计算即可。
任何人都可以帮我弄清楚为什么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:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background1"
android:gravity="fill"
android:orientation="vertical"
android:textColor="#ffffff"
tools:context=".CurrentPage" >
<TextView
android:id="@+id/Current_hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_marginTop="33dp"
android:text="Welcome to the Current Calculation page.
Hint - Use the check boxes to find the values that are missing "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/Equation_letter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/current_calculate"
android:layout_marginTop="37dp"
android:layout_toLeftOf="@+id/current_calculate"
android:text=" HELLO "
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/Distances_answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Equation_letter"
android:layout_alignBottom="@+id/Equation_letter"
android:layout_alignLeft="@+id/current_calculate"
android:layout_alignParentRight="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff" />
<TextView
android:id="@+id/t"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Q"
android:layout_alignRight="@+id/Q"
android:layout_below="@+id/Q"
android:layout_marginTop="36dp"
android:gravity="right|top"
android:text="t ="
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
<CheckBox
android:id="@+id/custom3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/t"
android:layout_alignLeft="@+id/custom2"
android:background="@drawable/check_box_new"
android:button="@drawable/check_box_new" />
<CheckBox
android:id="@+id/custom1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/number_input_3"
android:layout_alignLeft="@+id/custom3"
android:background="@drawable/check_box_new"
android:button="@drawable/check_box_new" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Current_hint"
android:layout_alignRight="@+id/Current_hint"
android:layout_below="@+id/Equation_letter"
android:layout_marginTop="25dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/CurrentmainHelp"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1.31"
android: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? "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="18sp"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/Yes_Please"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="36dp"
android:layout_weight="1.31"
android:onClick="CurrentHelp"
android:text="Yes Please"
android:textColor="#ffffff" />
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/current_calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/Calculate_Current"
android:textColor="#ffffff" />
<EditText
android:id="@+id/number_input_2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_above="@+id/t"
android:layout_alignLeft="@+id/number_input_1"
android:layout_alignRight="@+id/number_input_1"
android:ems="10"
android:focusable="true"
android:inputType="numberDecimal"
android:textColor="#ffffff" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/number_input_3"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/t"
android:layout_alignLeft="@+id/number_input_2"
android:layout_alignRight="@+id/number_input_2"
android:ems="10"
android:focusable="true"
android:inputType="numberDecimal"
android:textColor="#ffffff" />
<CheckBox
android:id="@+id/custom2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/number_input_1"
android:layout_marginLeft="45dp"
android:layout_toRightOf="@+id/number_input_1"
android:background="@drawable/check_box_new"
android:button="@drawable/check_box_new" />
<EditText
android:id="@+id/number_input_1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_above="@+id/Q"
android:layout_alignRight="@+id/current_calculate"
android:ems="10"
android:focusable="true"
android:inputType="numberDecimal"
android:singleLine="true"
android:textColor="#ffffff" />
<TextView
android:id="@+id/Q"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Equation_letter"
android:layout_below="@+id/I"
android:layout_marginTop="36dp"
android:gravity="right|top"
android:text="Q ="
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/I"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Q"
android:layout_alignRight="@+id/Q"
android:layout_below="@+id/Current_hint"
android:layout_marginTop="65dp"
android:gravity="right|top"
android:text="I ="
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
答案 0 :(得分:0)
我认为应用程序崩溃是因为您正在解析EditText
到double
的内容,即使它们可能为空。 Double.parseDouble
函数如果无法解析给定NumberFormatException
中的double
,则会抛出String
。要解决此问题,请在尝试解析之前执行空字符串检查,或捕获异常。
例如:
double charge = 0.0; // some default value
if (!Charge1.getText().toString().isEmpty()) {
charge = Double.parseDouble(Charge1.getText().toString());
}
double time = 0.0; // some default value
if (!Time1.getText().toString().isEmpty()) {
time = Double.parseDouble(Time1.getText().toString());
}
double current = 0.0; // some default value
if (!current1.getText().toString().isEmpty()) {
current = Double.parseDouble(current1.getText().toString());
}