【发布时间】:2014-11-25 20:33:04
【问题描述】:
感谢您的宝贵时间。
我已将ListView 扩展为:
public class PrettyListView extends ListView {
public PrettyListView(Context context) {
super(context);
}
public PrettyListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public PrettyListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
我在片段的上下文中并尝试像这样实例化它:
listView = (PrettyListView) rootView.findViewById(R.id.list);
我收到以下错误:
java.lang.ClassCastException: android.widget.ListView cannot be cast to com.sandbox.PrettyListView
请帮助我了解如何覆盖此错误,因为这是一个安全的演员表。
谢谢!
编辑:XML 按要求。再次感谢。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/wrapperView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.sandbox.MainActivity$PlaceholderFragment">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true" />
</RelativeLayout>
【问题讨论】:
-
问题出在你的 XML 文件中,也贴出来
-
在带有 id 列表的片段视图布局中不是 PrettyListView 的实例
-
为什么这被否决了?这似乎是一个足够合理的问题。
标签: java android oop inheritance constructor