Java android.widget.AutoTextComplete中的ClassCastException

Java android.widget.AutoTextComplete中的ClassCastException,java,android,android-layout,Java,Android,Android Layout,我无法将AutoCompleteText类强制转换为CustomAutoCompleteView。即使CustomAutoCompleteView类包含匹配的构造函数。它会引发运行时异常。请帮助我。 如果类B扩展A,则类B可以转换为类A,因为可以保证B的所有实例也是A的实例。但是,您不能保证A的实例是B,因此,如果您没有首先使用instanceof来验证A的实例实际上是B的实例,则从A到B的强制转换可以抛出ClassCastException 关于构造的语句(如果不适用),因为强制转换不使用构造

我无法将AutoCompleteText类强制转换为CustomAutoCompleteView。即使CustomAutoCompleteView类包含匹配的构造函数。它会引发运行时异常。请帮助我。


如果类
B
扩展
A
,则类
B
可以转换为类
A
,因为可以保证
B
的所有实例也是
A
的实例。但是,您不能保证
A
的实例是
B
,因此,如果您没有首先使用
instanceof
来验证
A
的实例实际上是
B
的实例,则从
A
B
的强制转换可以抛出
ClassCastException


关于构造的语句(如果不适用),因为强制转换不使用构造函数,并且对象已经存在。

这是my review.xml文件,您可以检查小部件的id。它类似于,<.CustomAutoCompleteView android:id=“@+id/autoCompleteTextView1”android:layout\u width=“match\u parent”android:layout\u height=“wrap\u content”android:layout\u alignParentLeft=“true”android:layout\u below=“@+id/textView1”android:text=“@string/restaurantname\u autocomplete”>查看我的评论并相应更改它。而不是
public class Review extends Activity {

    private CustomAutoCompleteView autoComplete;
    private ArrayAdapter<String> autoCompleteAdapter;

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

        System.out.print("1");
        autoCompleteAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line);
        System.out.print("2");
        autoCompleteAdapter.setNotifyOnChange(true); d
        autoComplete = (CustomAutoCompleteView) findViewById(R.id.autoCompleteTextView1);/*Line which is unable to cast throws Exception*/
        autoComplete.setHint("Restaurant Name");
        autoComplete.setThreshold(3);
        autoComplete.setAdapter(autoCompleteAdapter);
        autoComplete.addTextChangedListener(textChecker);

        final EditText et1=(EditText)findViewById(R.id.editText3);
}
public class CustomAutoCompleteView extends AutoCompleteTextView {
public CustomAutoCompleteView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public CustomAutoCompleteView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public CustomAutoCompleteView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
@Override
protected void performFiltering(final CharSequence text, final int keyCode) {
String filterText = "";
super.performFiltering(filterText, keyCode);
}
/**
* After a selection, capture the new value and append to the existing
* text
*/
@Override
protected void replaceText(final CharSequence text) {
super.replaceText(text);
}
}