Java 强制程序崩溃的If语句

Java 强制程序崩溃的If语句,java,android,eclipse,math,if-statement,Java,Android,Eclipse,Math,If Statement,我以前问过几个问题,但我决定回到基本问题上来 我正在创建一个数学android应用程序,每个页面有三个文本字段,用户可以在其中输入一个数字,我已经将这些文本字段解析为一个双精度文本。但是,假设用户在两个文本字段中输入一个数字,我希望程序告诉我第三个文本字段的值 目前,我可以让程序运行一次计算,但是当我为其余两个文本字段添加if语句时,当我单击calculate时,程序崩溃。 程序背后的关键是发现哪个文本字段为空,以便程序能够进行正确的计算 我当前正在尝试.getText.toString.isE

我以前问过几个问题,但我决定回到基本问题上来

我正在创建一个数学android应用程序,每个页面有三个文本字段,用户可以在其中输入一个数字,我已经将这些文本字段解析为一个双精度文本。但是,假设用户在两个文本字段中输入一个数字,我希望程序告诉我第三个文本字段的值

目前,我可以让程序运行一次计算,但是当我为其余两个文本字段添加if语句时,当我单击calculate时,程序崩溃。 程序背后的关键是发现哪个文本字段为空,以便程序能够进行正确的计算

我当前正在尝试.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: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" />

我认为应用程序崩溃是因为您正在将EditText的内容解析为双倍,即使它们可能是空的。如果Double.parseDouble函数无法解析给定字符串中的Double,它将抛出NumberFormatException。要解决此问题,请在尝试解析或捕获异常之前执行空字符串检查

例如:

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());
}

为了获得快速帮助。另外,post stacktrace.your应用程序因错误日志中明确说明的原因崩溃。请阅读。好吧,问题是我经常被批评太遵从sscce,因此我试图提供尽可能多的信息,对不起njzk2,什么是堆栈跟踪?当我说我是Java新手的时候,我是说noob newDude拜托,你可能是Java的一个巫师,知道如何应对eclipse,但我真的没有一罐胶水吗?你我的先生是一个真正的巫师,非常感谢你的款待,谢谢你对你的编程技能不那么傲慢,再次干杯,哦,我会尝试给你一些代表,但显然我不能:?所以也许其他人可以,干杯没问题!将来,您可以通过查看导致程序崩溃的原因来调试此类问题。检查IDE中的日志,查看红色文本的巨大块,这些块通常是异常堆栈跟踪。他们可以告诉你问题出在哪条线上。这里有一篇关于调试Android应用程序的文章:哦,为此干杯,Java并不是我最擅长的,我只是无法掌握它,我猜它并不适合所有人,干杯,Bugging是Java实际上是它最擅长的地方之一。下面是关于如何在Eclipse中查看堆栈跟踪控制台的信息:正如您所看到的,有一些文本说。。。并给出一个行号。这就是发生异常且未处理的地方。有关异常处理的信息,请参阅