2014-04-17 53 views
-2

我之前已經問了幾個問題,但是我已經決定回到基礎。如果聲明強制程序崩潰

我正在創建一個數學android應用程序,每個頁面有三個用戶可以輸入數字的TextField,我已經將這些TextField s解析爲一個double。但是,如果用戶在TextField中輸入了一個數字,我希望程序告訴我第三個文本字段的值。

目前我可以讓程序運行一次計算,但是當我爲其餘兩個TextField s添加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" /> 

+5

[試着問短的,獨立的,正確的(可編譯)問題(http://www.sscce.org/)快速幫助。 –

+4

也,發佈stacktrace。 – njzk2

+0

您的應用崩潰的原因在您的錯誤日誌中明確指出。請閱讀。 – njzk2

回答

0

我相信應用程序崩潰是因爲您將EditText的內容解析爲double,即使它們可能爲空。 Double.parseDouble函數將拋出NumberFormatException,如果它不能從給定的String中解析出double。要解決此問題,請在嘗試解析之前檢查空字符串,或者捕獲異常。

例如:

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

你我的先生是一個真正的嚮導,非常感謝你已經工作的一種享受,謝謝你沒有與你一樣傲慢的編程技巧,再次歡呼,哦,我會嘗試給你一些代表,但顯然我不能: ?所以也許別人可以,歡呼 – user3442326

+0

沒問題!將來,您可以通過查看造成程序崩潰的原因來調試此類問題。檢查您的IDE中的日誌是否有巨大的紅色文本塊(通常是異常堆棧跟蹤)。他們可以告訴你問題出在哪裏。這裏有一篇關於調試Android應用程序的文章:http://developer.android.com/tools/debugging/index.html – BVB

+0

哦,爲此歡呼,Java並不是我最強的套裝,我只是無法掌握它,猜測它不是對大家來說,歡呼的芽 – user3442326