0

我更改了一些我认为不相关的东西,运行了程序,然后遇到了这个错误:

    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

我在网上搜索,发现最有可能的情况是两个小部件具有相同的 id。我在我的 XML 中没有找到这个。这是XML:

<?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>

这是标记第 52 行的代码的一部分。如果您需要更多代码,我会放上来。很长,很遗憾。VectorsView 尚未完成,可能无法正常工作。这甚至不是我正在研究的程序的一部分。任何帮助都会很棒。

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

1 回答 1

2

引起:java.lang.ClassCastException:android.widget.TextView

您正在尝试将 aTextView转换为 a LinearLayout。但是您的 XML 似乎是正确的,因此请检查清理整个项目是否有帮助,有时Eclipse/ADT会搞砸并且无法正确重建资源,从而导致 id 指向错误的位置。

于 2012-06-05T22:09:05.260 回答