2014-04-17 1 views
-2

몇 가지 질문을했지만 이전에 다시 돌아가보기로했습니다.문장이 강제로 프로그램을 강제 실행하는 경우

나는 수학적 안드로이드 응용 프로그램을 만들고 있는데, 각 페이지는 사용자가 숫자를 입력 할 수있는 세 개의 TextField을 가지고 있으며, 나는이 두 글자를 두 번 파싱했습니다. 그러나 사용자가 TextField 중 2 개에 숫자를 입력했다고 가정하면 프로그램에서 세 번째 텍스트 필드의 값을 알려주고 싶습니다.

현재 하나의 계산을 실행하는 프로그램을 얻을 수 있지만 나머지 두 개의 if 문을 추가 할 때초를 계산할 때 계산을 클릭하면 프로그램이 충돌합니다. 프로그램의 핵심은 프로그램이 올바른 계산을 할 수 있도록 TextField이 비어 있는지 확인하는 것입니다.

나는 현재 .getText().toString().isEmpty()을 시도하고 있지만 작동하지 않습니다. 또한 시도했습니다 .length() == 0, 그러나 이것도 작동하지 않습니다.

에뮬레이터를 실행하고 계산기 단추를 클릭하면 더 많은 변수를 추가 할 때 프로그램이 중단되지만 단 하나의 계산으로도 문제가 없습니다.

누군가가 왜 If 문이 작동하지 않는지 파악하고 가능한 경우 누구든지 솔루션을 제공 할 수 있습니까? 내 서식이 꺼져있는 경우

, 나는 아래의 자바 인, 자바 비교적 새로운 미안 해요 나는 또한이 필요한 경우에는 XML을 제공 한

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

     } 
    }); 

<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/) [짧은, 콘도, 수정 (에 Compilable) 질문을보십시오]. –

+4

또한 게시 스택 추적. – njzk2

+0

오류 로그에 명백하게 명시된 이유로 앱이 다운됩니다. 그것을 읽으십시오. – njzk2

답변

0

EditText의 내용을 double으로 파싱하기 때문에 응용 프로그램이 손상 될 수 있습니다. 지정된 String에서 double을 구문 분석 할 수없는 경우 Double.parseDouble 함수는 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()); 
} 
+0

당신은 내 선생님이 정품 마법사입니다, 대단한 일을 해 주셔서 대단히 감사합니다, 프로그래밍 기술과 오만이 아닌 것에 대해 감사드립니다, 다시 환호하고, 아, ?어쩌면 다른 사람도 할 수있어, 환호 – user3442326

+0

괜찮아! 앞으로 프로그램 충돌을 일으키는 원인을 조사하여 이러한 문제를 디버깅 할 수 있습니다. 빨간색 텍스트의 거대한 블록 (일반적으로 예외 스택 추적)에 대한 IDE의 로그를 확인하십시오. 문제가있는 줄을 알려줄 수 있습니다. Android 애플리케이션 디버깅에 대한 기사는 다음과 같습니다. http://developer.android.com/tools/debugging/index.html – BVB

+0

오, 그걸 위해 건배는 자바가 내게 가장 강한 것이 아니며, 그저 손을 잡을 수는 없습니다. 모두를 위해, 축하합니다. – user3442326

관련 문제