Android 尝试使用view.GetChildAt将视图强制转换为包装类时出错

Android 尝试使用view.GetChildAt将视图强制转换为包装类时出错,android,Android,我试图在使用View.GetChildAt时返回一个包装器类,但它给了我一个非法的CastException。我知道我试图返回的孩子是一个编辑文本 MyEditText firstName = (MyEditText)infoLayout.GetChildAt(3); 注意:在这种情况下,我不能使用FindByID 我猜这与尝试将基类转换为派生类有关,但我希望有一个解决方法 这是我的xml布局 <?xml version="1.0" encoding="utf-8"?> <R

我试图在使用View.GetChildAt时返回一个包装器类,但它给了我一个非法的CastException。我知道我试图返回的孩子是一个编辑文本

MyEditText firstName = (MyEditText)infoLayout.GetChildAt(3);
注意:在这种情况下,我不能使用FindByID

我猜这与尝试将基类转换为派生类有关,但我希望有一个解决方法

这是我的xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/relativeLayout1">
<TextView
    android:text="Type"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView1"
    android:textSize="20dp"
    android:layout_marginTop="30dp"
    android:layout_marginLeft="10dp" />
<Spinner
    android:layout_width="200dp"
    android:layout_height="45dp"
    android:layout_toRightOf="@id/textView1"
    android:id="@+id/sp_type"
    android:layout_alignBottom="@id/textView1"
    android:layout_marginLeft="20dp" />
<TextView
    android:text="First Name"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView2"
    android:textSize="20dp"
    android:layout_marginTop="20dp"
    android:layout_below="@id/textView1"
    android:layout_alignLeft="@id/textView1" />
<EditText
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/textView2"
    android:id="@+id/et_firstName"
    android:layout_alignBottom="@id/textView2"
    android:layout_marginLeft="10dp" />
</RelativeLayout>


如果它不是MyEditText类型,为什么要对它进行类型转换?它说它是什么类?可以发布布局吗?它是GetChildAt返回的EditText类。我想使用MyEditText类,它是我需要的一些额外功能的包装器。明白了。我必须将xml中的EditText更改为我的包装类
,还必须将EditText(上下文上下文,AttributeSet attrs)构造函数添加到我的包装类中