![](/img/trans.png)
[英]Android Filter searchView Widget for scala “ got ClassCastException”
[英]ClassCastException in android.widget.AutoTextComplete
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);
}
}
我无法将 AutoCompleteText class 转换为 CustomAutoCompleteView。即使 class CustomAutoCompleteView 包含匹配的构造函数。它会引发运行时异常。请帮助我。
如果 class B
扩展A
则 class B
可以转换为 class A
因为可以保证B
的所有实例也是A
的实例。 但是,您不能保证A
的实例是B
因此,如果您没有首先使用instanceof
来验证A
的实例实际上是B
的实例,则从A
到B
的强制转换可能会抛出ClassCastException
。
关于构造的声明如果不适用,因为转换不使用构造函数并且 object 已经存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.