0

這是一個Java中的小費計算器應用程序。如果這兩個字段沒有填寫,它應該在TextView中顯示錯誤消息,但它只是凍結應用程序...任何提示?如果editText爲空,則結束方法

package tip.calculator; 


import android.os.Bundle; 
import android.app.Activity; 
import android.view.View.OnClickListener; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.view.View; 
import android.widget.Button; 




public class TipCalculator extends Activity 
{ 

private Button enter; 
EditText myEditField ; 
EditText myEditField2; 
float percentage = 0; 
float percentageInp = 0; 
float billAmount = 0; 
double output = 0; 
String output1 = ""; 
Button clearButton ; 
TextView textView; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    myEditField = (EditText) findViewById(R.id.percentText); 
    enter = (Button)findViewById(R.id.button1); 
    myEditField2 = (EditText) findViewById(R.id.billText); 
    clearButton = (Button) findViewById(R.id.clearButton); 


    enter.setOnClickListener(new OnClickListener() { 


     public void onClick(View v) { 

      TextView errors; 
      textView = (TextView) findViewById(R.id.textView1); 
      errors = (TextView) findViewById(R.id.errorText);  




      if(myEditField.getText().equals("")){ 
       errors.setText("Percent must be filled in"); 


      } 

      if(myEditField.getText().equals("")){ 
       errors.setText("Bill Amount must be filled in"); 

      } 




      percentageInp = Float.parseFloat(myEditField.getText().toString()); 
      billAmount = Float.parseFloat(myEditField2.getText().toString()); 

      percentage = ((float)percentageInp /100); 

      output = (double)(billAmount * percentage); 

      double result = output * 100; 
      result = Math.round(result); 
      result = result/100; 

      output1 = Double.toString(result); 

      textView.setText(output1 + " $"); 

     } 
    }); 

    clearButton.setOnClickListener(new OnClickListener() { 

     public void onClick(View arg0) { 

      percentage = 0; 
      output = 0; 
      output1 = ""; 

      TextView textView = (TextView)findViewById(R.id.errorText); 
      textView.setText(""); 

      TextView textView2 = (TextView)findViewById(R.id.percentText); 
      textView2.setText(""); 

      TextView textView3 = (TextView)findViewById(R.id.billText); 
      textView3.setText(""); 


      TextView textView1 = (TextView)findViewById(R.id.textView1); 
      textView1.setText(""); 


      percentageInp = 0; 
      billAmount = 0; 

      myEditField.clearComposingText(); 
      myEditField2.clearComposingText(); 



      return; 
     } 


    }); 
}* 

}

這裏是(按要求提供)佈局的xml ...

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/errorText" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:onClick="onEnterClick" > 

    <EditText 
     android:id="@+id/billText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:ems="10" 
     android:hint="Bill Amount" 
     android:inputType="numberDecimal|textAutoComplete" 
     android:singleLine="true" /> 

    <EditText 
     android:id="@+id/percentText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/billText" 
     android:layout_below="@+id/billText" 
     android:ems="10" 
     android:hint="Percent" 
     android:inputType="number" 
     android:singleLine="true" > 

     <requestFocus /> 
    </EditText> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/percentText" 
     android:layout_centerHorizontal="true" 
     android:text="Calculate" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/percentText" 
     android:layout_toRightOf="@+id/billText" 
     android:text="$" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/percentText" 
     android:layout_alignLeft="@+id/textView2" 
     android:text="%" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <Button 
     android:id="@+id/clearButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/button1" 
     android:layout_centerHorizontal="true" 
     android:text="Clear" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/textView3" 
     android:layout_below="@+id/clearButton" 
     android:text="Solution Will Appear Here" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/button1" 
     android:layout_below="@+id/errorText" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

</RelativeLayout> 

這裏是logcat的輸出(僅錯誤)

08-01 20:49:08.424: E/(13643): URE: SkPaint in getMeasureCacheProc index: 2 
08-01 20:49:08.689: E/(13643): URE: SkPaint in getMeasureCacheProc index: 2 
08-01 20:49:10.844: E/(13643): URE: SkPaint in getMeasureCacheProc index: 2 
08-01 20:49:12.074: E/(13643): URE: SkPaint in getMeasureCacheProc index: 2 
08-01 20:49:12.254: E/(13643): URE: SkPaint in getMeasureCacheProc index: 2 
08-01 20:49:16.649: E/AndroidRuntime(13643): FATAL EXCEPTION: main 
08-01 20:49:16.649: E/AndroidRuntime(13643): java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView 
08-01 20:49:16.649: E/AndroidRuntime(13643): at tip.calculator.TipCalculator$2.onClick(TipCalculator.java:91) 
08-01 20:49:16.649: E/AndroidRuntime(13643): at android.view.View.performClick(View.java:3620) 
08-01 20:49:16.649: E/AndroidRuntime(13643): at android.view.View$PerformClick.run(View.java:14292) 
08-01 20:49:16.649: E/AndroidRuntime(13643): at android.os.Handler.handleCallback(Handler.java:605) 
08-01 20:49:16.649: E/AndroidRuntime(13643): at android.os.Handler.dispatchMessage(Handler.java:92) 
08-01 20:49:16.649: E/AndroidRuntime(13643): at android.os.Looper.loop(Looper.java:137) 
08-01 20:49:16.649: E/AndroidRuntime(13643): at android.app.ActivityThread.main(ActivityThread.java:4507) 
08-01 20:49:16.649: E/AndroidRuntime(13643): at java.lang.reflect.Method.invokeNative(Native Method) 
08-01 20:49:16.649: E/AndroidRuntime(13643): at java.lang.reflect.Method.invoke(Method.java:511) 
08-01 20:49:16.649: E/AndroidRuntime(13643): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) 
08-01 20:49:16.649: E/AndroidRuntime(13643): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) 
08-01 20:49:16.649: E/AndroidRuntime(13643): at dalvik.system.NativeStart.main(Native Method) 
+0

parseFloat()返回一個空字符串是什麼? – Iain 2012-08-02 03:36:57

回答

0

從你的logcat,我明白了沒有TextView與ID ERRORTEXT,但你看不到任何錯誤,在編譯的時候,你有這個編號的RelativeLayout;但在運行時會出現ClassCastException錯誤。

errors = (TextView) findViewById(R.id.errorText); 

此外,始終使用""..equals(myEditField.getText())代替myEditField.getText().equals("")(即,硬編碼值第一)。

否則,如果您EditText是空的,myEditField.getText()將返回,所以你有一個錯誤嘗試使用equals()方法上

或者,您可以使用myEditField.getText().length() = 0來檢查EditText是否爲空。

0

爲什麼您在onClickreturn?你也有*在倒數第二}我不知道這是否能解決您的問題,但它是引起了我的注意。