简体   繁体   English

LogCat错误,为什么会发生?

[英]LogCat error, why does this occur?

here is the java file that produces the error "08-02 19:17:22.265: E/AndroidRuntime(17817): java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView" 这是产生错误的Java文件“ 08-02 19:17:22.265:E / AndroidRuntime(17817):java.lang.ClassCastException:android.widget.RelativeLayout无法转换为android.widget.TextView”

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

        }


    });
}
}

here is the logcat error that i don't understand why it occurs... 这是我不明白为什么会发生的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)

here is the xml with all the textViews and such sorry took a while... 这是带有所有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>

There are a lot of errors here. 这里有很多错误。 You have to match the xml view types in your code. 您必须在代码中匹配xml视图类型。

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

I'm not sure what you are trying to do here but TextView's must be cast to TextView's. 我不确定您要在这里做什么,但必须将TextView强制转换为TextView。 EditText's must be cast to EditText's. 必须将EditText强制转换为EditText。 etc... 等等...

UPDATED 更新

Show me your xml file that you are inflating. 告诉我您正在膨胀的xml文件。 One of the View IDs is for a RelativeLayout and you are trying to cast it to a TextView. 视图ID之一是用于RelativeLayout的,您正尝试将其强制转换为TextView。

One of these R.id's are for a RelativeLayout: 这些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);

You can not compare strings with == or != in java instead use .equals() 您不能在Java中用==!=比较字符串,而是使用.equals()

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

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM