简体   繁体   English

java.lang.ClassCastException:android.widget.TextView

[英]java.lang.ClassCastException: android.widget.TextView

I've this layout xml, named pagina.xml: 我有这个布局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>

I want to write the description of the element, but when I assign to a variable the textview element, the emulator crashes. 我想编写元素的描述,但是当我将textview元素赋给变量时,模拟器会崩溃。 This is the code: 这是代码:

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

    }

Help me to understand! 帮帮我理解!

Try 尝试

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

instead of 代替

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

You are using TextView in xml layout. 您正在使用xml布局中的TextView But you tried to cast this object to EditText 但是您尝试将此对象EditTextEditText

What are you going to use? 你打算用什么? If you want this text to be editable you need to change your component to EditText in xml and in your class. 如果您希望此文本可编辑,则需要将组件更改为xml和类中的EditText

  • TextView - just prints text TextView - 只打印文本
  • EditText - lets you to edit text in program EditText - 允许您编辑程序中的文本

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException:android.widget.TextView无法转换为android.widget.EditText - java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText java.lang.ClassCastException:android.widget.ScrollView无法强制转换为android.widget.TextView - java.lang.ClassCastException: android.widget.ScrollView cannot be cast to android.widget.TextView java.lang.ClassCastException:android.widget.ImageButton无法转换为android.widget.TextView - java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.TextView java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView 不能转换为 android.widget.TextView - java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.TextView java.lang.ClassCastException:android.support.constraint.ConstraintLayout无法转换为android.widget.TextView - java.lang.ClassCastException: android.support.constraint.ConstraintLayout cannot be cast to android.widget.TextView 我不断收到错误 java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.TextView - i keep getting the error java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.TextView 来自android的java.lang.classcastexception android.widget.textview错误,但我看不到 - java.lang.classcastexception android.widget.textview error from android yet I can't see how java.lang.ClassCastException:android.widget.TextView。我为什么要这个? - java.lang.ClassCastException: android.widget.TextView. Why am I getting this? java.lang.ClassCastException: android.widget.EditText - java.lang.ClassCastException: android.widget.EditText android中的java.lang.ClassCastException - java.lang.ClassCastException in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM