简体   繁体   中英

Why am I getting this ClassCastException (Android)?

I changed a couple things I thought were unrelated, ran the program, and ran into this error:

    06-05 14:53:33.286: ERROR/AndroidRuntime(29084): FATAL EXCEPTION: main
06-05 14:53:33.286: ERROR/AndroidRuntime(29084): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.prattia.webs.cheaterphysics/com.prattia.webs.cheaterphysics.Vectors}: java.lang.ClassCastException: android.widget.TextView
06-05 14:53:33.286: ERROR/AndroidRuntime(29084):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084):     at android.os.Looper.loop(Looper.java:123)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084):     at java.lang.reflect.Method.invokeNative(Native Method)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084):     at java.lang.reflect.Method.invoke(Method.java:521)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084):     at dalvik.system.NativeStart.main(Native Method)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084): Caused by: java.lang.ClassCastException: android.widget.TextView
06-05 14:53:33.286: ERROR/AndroidRuntime(29084):     at com.prattia.webs.cheaterphysics.Vectors.onCreate(Vectors.java:52)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084):     ... 11 more

I searched online and discovered that the most likely scenario was that two widgets had the same id. I didn't find this in my XML. Here is the XML though:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" 
    android:layout_width="match_parent">
    <LinearLayout 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent"
         android:background="@drawable/background" 
         android:orientation="vertical">

         <ImageView android:layout_height="wrap_content" 
            android:src="@drawable/vectors" 
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:adjustViewBounds="true">
         </ImageView>
         <LinearLayout android:layout_height="wrap_content" 
            android:layout_width="match_parent"
            android:orientation="horizontal">
            <Button android:text="Choose Program" 
                android:id="@+id/bChsProgV" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="97dp"
                android:adjustViewBounds="true">
            </Button>
            <ImageButton android:layout_height="wrap_content" 
                android:src="@drawable/help" 
                android:id="@+id/ibHelpV" 
                android:layout_width="wrap_content"
                android:layout_marginLeft="65dp"
                android:background="@null" 
                android:layout_marginTop="10dp">
            </ImageButton>
         </LinearLayout>
         <LinearLayout android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
            android:orientation="horizontal">
             <TextView android:text="Value" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:textColor="#fffafa">
             </TextView>
             <EditText android:text="" 
                android:id="@+id/etValueV" 
                android:layout_height="wrap_content"
                android:inputType="numberSigned|numberDecimal"
                android:layout_width="100dp" >
             </EditText>
             <Button android:text="Next" 
                android:id="@+id/bNextV" 
                android:layout_width="65dp" 
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp">
             </Button>
         </LinearLayout>
         <LinearLayout android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
            android:orientation="horizontal">
             <TextView android:text="Angle" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:textColor="#fffafa">
             </TextView>
             <EditText android:text="" 
                android:id="@+id/etAngleV" 
                android:layout_height="wrap_content"
                android:inputType="numberSigned|numberDecimal"
                android:layout_width="100dp" >
             </EditText>
             <Button android:text="Prev" 
                android:id="@+id/bPrevV" 
                android:layout_width="65dp" 
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp">
             </Button>
         </LinearLayout>
        <TextView android:text="" 
            android:id="@+id/tvAnsValV"
            android:textColor="#fffafa" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_gravity="left"
            android:paddingLeft="5dp">
         </TextView>
         <LinearLayout android:layout_height="wrap_content" 
            android:layout_width="match_parent"
            android:orientation="horizontal">
            <LinearLayout android:layout_height="wrap_content" 
            android:layout_width="wrap_content"
            android:orientation="vertical">
                <RadioGroup
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >              
                    <RadioButton android:layout_height="wrap_content"
                        android:layout_width="wrap_content"
                        android:checked="true"
                        android:text="Degrees"
                        android:layout_marginLeft="5dp"
                        android:id="@+id/rbDegV">
                    </RadioButton>
                    <RadioButton android:layout_height="wrap_content"
                        android:layout_width="wrap_content"
                        android:checked="false"
                        android:text="Radians"
                        android:layout_marginLeft="5dp"
                        android:id="@+id/rbRadV">
                    </RadioButton>
                </RadioGroup>
            </LinearLayout>
            <RadioButton android:text="π" 
                android:id="@+id/rbPiV" 
                android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                android:checked="false"
                android:visibility="invisible"
                android:layout_marginTop="50dp">
            </RadioButton> 
            <RadioGroup
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >  
                <LinearLayout android:layout_height="wrap_content" 
                    android:layout_width="wrap_content"
                    android:orientation="horizontal">
                    <LinearLayout android:layout_height="wrap_content" 
                        android:layout_width="wrap_content"
                        android:orientation="vertical">
                        <RadioButton android:layout_height="wrap_content"
                            android:layout_width="wrap_content"
                            android:checked="false"
                            android:text="Q2"
                            android:layout_marginLeft="30dp"
                            android:id="@+id/rbQ2V">
                        </RadioButton>
                        <RadioButton android:layout_height="wrap_content"
                                android:layout_width="wrap_content"
                                android:checked="false"
                                android:text="Q3"
                                android:layout_marginLeft="30dp"
                                android:id="@+id/rbQ3V">
                        </RadioButton>
                    </LinearLayout>
                    <LinearLayout android:layout_height="wrap_content" 
                        android:layout_width="wrap_content"
                        android:orientation="vertical">
                        <RadioButton android:layout_height="wrap_content"
                            android:layout_width="wrap_content"
                            android:checked="true"
                            android:text="Q1"
                            android:layout_marginLeft="5dp"
                            android:id="@+id/rbQ1V">
                        </RadioButton>
                        <RadioButton android:layout_height="wrap_content"
                                android:layout_width="wrap_content"
                                android:checked="false"
                                android:text="Q4"
                                android:layout_marginLeft="5dp"
                                android:id="@+id/rbQ4V">
                        </RadioButton>
                    </LinearLayout>
                </LinearLayout> 
            </RadioGroup>                                                       
         </LinearLayout>
         <Button android:text="Submit" 
            android:id="@+id/bSubmitV" 
            android:layout_width="75dp" 
            android:layout_height="wrap_content"
            android:layout_gravity="center">
         </Button>
         <TextView android:text="" 
            android:id="@+id/tvErrorV" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textColor="#fffafa">
         </TextView>
         <LinearLayout android:id="@+id/llCanvasV" 
            android:layout_height="wrap_content" 
            android:layout_width="match_parent">
         </LinearLayout>
    </LinearLayout>

</ScrollView>

And here is a portion of the code with line 52 marked. If you need more code I'll put it up. It's long, unfortunately. VectorsView is not yet finished and likely doesn't work. It wasn't even the portion of the program I was working on. Any help would be great.

  public void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.vectors);
            vectorsView = new VectorsView(this);
            l = (LinearLayout) findViewById(R.id.llCanvasV);  //Line 52
            l.addView(vectorsView);
...}

public class VectorsView extends View{

        public VectorsView(Context context) {
            super(context);
        }
        @Override
        protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub
            super.onDraw(canvas);       

            Paint paint = new Paint();
            paint.setColor(Color.WHITE);

            canvas.drawLine(0, 0, 100, 100, paint);
            vectorsView.draw(canvas);
            vectorsView.setLayoutParams(new LayoutParams(25, 25)); 
        }
    }

public void Initialize(){
        chsprog = (Button) findViewById(R.id.bChsProgV);
        next = (Button) findViewById(R.id.bNextV);
        prev = (Button) findViewById(R.id.bPrevV);
        submit = (Button) findViewById(R.id.bSubmitV);
        value = (EditText) findViewById(R.id.etValueV);
        angle = (EditText) findViewById(R.id.etAngleV);
        help = (ImageButton) findViewById(R.id.ibHelpV);
        deg = (RadioButton) findViewById(R.id.rbDegV);
        rad = (RadioButton) findViewById(R.id.rbRadV);
        q1 = (RadioButton) findViewById(R.id.rbQ1V);
        q2 = (RadioButton) findViewById(R.id.rbQ2V);
        q3 = (RadioButton) findViewById(R.id.rbQ3V);
        q4 = (RadioButton) findViewById(R.id.rbQ4V);
        pi = (RadioButton) findViewById(R.id.rbPiV);
        error = (TextView) findViewById(R.id.tvErrorV);
        ansval = (TextView) findViewById(R.id.tvAnsValV);
    }

Caused by: java.lang.ClassCastException: android.widget.TextView

You are trying to cast a TextView to a LinearLayout . But your XML seems correct, so check out if Cleaning the entire project helps, sometimes Eclipse/ADT messes up and does not rebuild the resources properly, causing ids to point to the wrong place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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