2012-08-03 117 views
0

這裏是生成錯誤「08-02 19:17:22.265:E/AndroidRuntime(17817):java.lang.ClassCastException:android.widget.RelativeLayout無法轉換爲android.widget.TextView」LogCat錯誤,爲什麼會發生這種情況?

package tip.calculator; 


import android.os.Bundle; 
import android.app.Activity; 
import android.text.TextUtils; 
import android.view.View.OnClickListener; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 
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((myEditField2 != null) && (!TextUtils.isEmpty(myEditField2.getText().toString()))){ 


       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 + " $"); 

      } else{ 
       //Toast.makeToast(.....).show(); //Inform user about the error 
      } 

     } 
    }); 

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

     } 


    }); 
} 
} 

這裏是logcat的錯誤,我不明白爲什麼它發生......

08-02 19:17:22.265: E/AndroidRuntime(17817): java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView 
08-02 19:17:22.265: E/AndroidRuntime(17817): at tip.calculator.TipCalculator$1.onClick(TipCalculator.java:48) 
08-02 19:17:22.265: E/AndroidRuntime(17817): at android.view.View.performClick(View.java:3620) 
08-02 19:17:22.265: E/AndroidRuntime(17817): at android.view.View$PerformClick.run(View.java:14292) 
08-02 19:17:22.265: E/AndroidRuntime(17817): at android.os.Handler.handleCallback(Handler.java:605) 
08-02 19:17:22.265: E/AndroidRuntime(17817): at android.os.Handler.dispatchMessage(Handler.java:92) 
08-02 19:17:22.265: E/AndroidRuntime(17817): at android.os.Looper.loop(Looper.java:137) 
08-02 19:17:22.265: E/AndroidRuntime(17817): at android.app.ActivityThread.main(ActivityThread.java:4507) 
08-02 19:17:22.265: E/AndroidRuntime(17817): at java.lang.reflect.Method.invokeNative(Native Method) 
08-02 19:17:22.265: E/AndroidRuntime(17817): at java.lang.reflect.Method.invoke(Method.java:511) 
08-02 19:17:22.265: E/AndroidRuntime(17817): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) 
08-02 19:17:22.265: E/AndroidRuntime(17817): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) 
08-02 19:17:22.265: E/AndroidRuntime(17817): at dalvik.system.NativeStart.main(Native Method) 

這裏是所有的textViews和這樣的遺憾了好一會兒的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" 
     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" /> 

</RelativeLayout> 
+0

什麼是線48 TipCalculator.java? – Sam 2012-08-03 02:32:12

回答

1

你不能在Java ==!=比較字符串,而不是使用.equals()

if(myEditField2.getText().toString().equals("")) && (!TextUtils.isEmpty(myEditField2.getText().toString()))) 

希望有所幫助。

+0

雖然這是比較字符串的正確方法,但您有一些錯誤。首先是'myEditField2.equals(「」)'應該是'myEditField2.getText()。toString()。equals(「」)''。另一個是這不會導致ClassCastException。 – Sam 2012-08-03 02:35:58

+0

是的,謝謝你的糾正。 – 0gravity 2012-08-03 02:37:49

2

這裏有很多錯誤。您必須在代碼中匹配xml視圖類型。

errors = (RelativeLayout)findViewById(R.id.errorText); 
EditText textView3 = (EditText)findViewById(R.id.billText); 
EditText textView2 = (EditText)findViewById(R.id.percentText); 

我不知道你正試圖在這裏做的,但TextView中的必須強制轉換的TextView的。 EditText的必須轉換爲EditText的。等等......

修訂

告訴我,你是充氣的XML文件。其中一個視圖ID用於RelativeLayout,並且您試圖將其轉換爲TextView。

其中一個R.id的是一個RelativeLayout的的:

textView = (TextView) findViewById(R.id.textView1); 
errors = (TextView)findViewById(R.id.errorText); 
TextView textView = (TextView)findViewById(R.id.errorText); 
TextView textView2 = (TextView)findViewById(R.id.percentText); 
TextView textView3 = (TextView)findViewById(R.id.billText); 
TextView textView1 = (TextView)findViewById(R.id.textView1); 
+0

由於錯誤在OnClickListener中,我猜測它是R.id.textView1或R.id.errorText。很高興看到XML並且知道確切信息。 – Sam 2012-08-03 02:41:39

+0

我同意。這些都是OnClickListener裏面的兩個casts,在48行附近放了 – Spidy 2012-08-03 02:51:24

+0

xml,對不起花了這麼長時間,正在吃東西.. – PrommeringsDisplay 2012-08-03 03:01:44

相關問題