2012-06-05 2 views
0

Я изменил несколько вещей, я думал, были связаны между собой, запускал программу, и столкнулся с этой ошибкой:Почему я получаю это ClassCastException (Android)?

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 

Я искал в Интернете и обнаружил, что наиболее вероятный сценарий развития событий в том, что два виджета имел один и тот же идентификатор. Я не нашел это в своем 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); 
    } 

ответ

2

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

Вы пытаетесь бросить TextView к LinearLayout. Но ваш XML кажется правильным, поэтому проверьте, Очистка всего проекта помогает, иногда Eclipse/ADT беспорядок перестраивает и не восстанавливает ресурсы должным образом, заставляя идентификаторы указывать на то место.

Смежные вопросы