java.lang.ClassCastException:android.widget.TextView

时间:2012-06-27 16:57:47

标签: java android android-layout

我有这个布局xml,名为pagina.xml:

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

    }

帮助我理解!

1 个答案:

答案 0 :(得分:2)

尝试

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

而不是

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

您在xml布局中使用TextView。但是您尝试将此对象强制转换为EditText

你打算用什么?如果您希望此文字可编辑,则需要在xml和班级中将组件更改为EditText

  • TextView - 只打印文字
  • EditText - 允许您编辑程序中的文本