2012-06-27 2 views
0

Я этот макет XML, названный pagina.xml:java.lang.ClassCastException: android.widget.TextView

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center_vertical" > 

    <TextView 
     android:id="@+id/descrizione" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <Button 
     android:id="@+id/scelta1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/scelta3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/scelta1" 
     android:layout_alignLeft="@+id/scelta1" 
     android:layout_marginBottom="16dp" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/scelta2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/scelta3" 
     android:layout_alignLeft="@+id/scelta3" 
     android:layout_marginBottom="18dp" 
     android:text="Button" /> 

</RelativeLayout> 

Я хочу написать описание элемента, но когда я присвоить переменной элемент textview, сбой эмулятора. Это код:

public int posizione; 
    public String stanza; 
    public string[] azioni; 
    public int[] vai; 
    public XmlPullParser xpp; 
    public EditText descr; 
    public Button scelta_1, scelta_2, scelta_3; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.pagina); 

     try { 
      carica_stanza(posizione); 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (XmlPullParserException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     descr = (EditText) this.findViewById(R.id.descrizione); 
     scelta_1= (Button) this.findViewById(R.id.scelta1); 
     scelta_2= (Button) this.findViewById(R.id.scelta2); 
     scelta_3= (Button) this.findViewById(R.id.scelta3); 

    } 

Помогите мне разобраться!

+0

с помощью 'this' клавиатуры не невероятно большой стиль, на мой взгляд ... просто избавиться из этого. очевидно, что вы вызываете метод 'findViewById'' Activity. –

ответ

2

Попробуйте

public TextView descr; 
// ... 
descr = (TextView) this.findViewById(R.id.descrizione); 

вместо

public EditText descr; 
// ... 
descr = (EditText) this.findViewById(R.id.descrizione); 

Вы используете TextView в макете XML. Но вы пытались бросить этот объект на EditText

Что вы собираетесь использовать? Если вы хотите, чтобы этот текст редактировался, вам нужно изменить свой компонент на EditText в xml и в вашем классе.

  • TextView - просто печатает текст,
  • EditText - позволяет вам редактировать текст в программе
+0

+ измените свой тип descr на EditText. –

+0

doh! Проклятый вырезать и вставить !!!!!!! :( –

+1

Некоторые ошибки просто говорят вам, в чем проблема. –

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