java.lang.ClassCastException : android. widget.TextView

标签 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);

    }

帮我理解一下!

最佳答案

尝试

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 - 允许您在程序中编辑文本

关于java.lang.ClassCastException : android. widget.TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11231370/

相关文章:

java - Android用户权限错误(android.permission.ACCESS_WIFI_STATE)

java - Android 上解密数据库的临时存储

android - 更换工具栏后汉堡图标未正确显示

java - 尽管插入了行,但 Get generatedKeys 不起作用

java - 创建方法是否被视为抽象?

java - Android Firebase 检查数据是否等于字符串

android - Android 在消耗多少内存后抛出 OutOfMemoryException

java - 设置 X-Content-Type-Options 响应 header 会损坏 PNG 文件

java - Retrofit 调用包含错误 必需的 Retrofit2.Call,发现无效

android - 如何使用 drawable 兼容所有屏幕尺寸(idpi、mdpi、hdpi、xhdpi、xxhdpi)