I take text view instance from here:
TextView date = null;
try {
date = (TextView) getLayoutInflater().inflate(
R.layout.some_textview, null);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
I created my custom textview:
public class MyTextView extends TextView{
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyTextView(Context context) {
super(context);
}
}
Now i want to cast those:
MyTextView my = (MyTextView)date;
I get exeption for this one:
java.lang.ClassCastException: android.widget.TextView cannot be cast to com.myapp.name.MyTextView
So how it should be done ?
Thanks.
Edit:
If i declare date as MyTextView, still get same exception, ther is my xml of some_textview:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is a template for date text view"
/>
Is your XML-layout
R.layout.some_textviewresource correct?Don’t use
You have to use your custom class in your XML:
It’s very important the class-path is correct!