自己写ListView后,在onCreat()里findViewById获得listview后强转成自己写的listview时报ClassCaseException:报错
自己写自定义ListView类后,在onCreat()里findViewById获得View后强转成自己写的listview时报ClassCaseException
麻烦大家帮我看看,很急,已经赶了三天三夜了
MainActicity.java
package lzy.app.jobSystem; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { /** Called when the activity is first created. */ public TestAdapter adapter; public PinnedHeaderListView listView; @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //adapter = new TestAdapter(getLayoutInflater()); adapter = new TestAdapter(this); // System.out.println(this.findViewById(R.id.section_list_view)); listView = (PinnedHeaderListView) this.findViewById(R.id.section_list_view); listView.setAdapter(adapter); listView.setOnScrollListener(adapter); listView.setPinnedHeaderView(getLayoutInflater().inflate( R.layout.section_list_item, listView, false)); } }
ClassCaseException异常定位在这句listView = (PinnedHeaderListView) this.findViewById(R.id.section_list_view);
自己的ListView类:
package lzy.app.jobSystem; import android.content.Context; import android.graphics.Canvas; import android.util.AttributeSet; import android.view.View; import android.widget.Adapter; import android.widget.AdapterView; import android.widget.ListAdapter; import android.widget.ListView; public class PinnedHeaderListView extends ListView { public interface PinnedHeaderAdapter { public static final int PINNED_HEADER_GONE = 0; public static final int PINNED_HEADER_VISIBLE = 1; public static final int PINNED_HEADER_PUSHED_UP = 2; int getPinnedHeaderState(int position); void configurePinnedHeader(View header, int position); } private static final int MAX_ALPHA = 255; private PinnedHeaderAdapter mAdapter; /** 显示在顶端的item */ private View mHeaderView; private boolean mHeaderViewVisible; private int mHeaderViewWidth; private int mHeaderViewHeight; public PinnedHeaderListView(Context context) { super(context); } public PinnedHeaderListView(Context context, AttributeSet attrs) { super(context, attrs); } public PinnedHeaderListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public void setPinnedHeaderView(View view) { mHeaderView = view; // Disable vertical fading when the pinned header is present // TODO change ListView to allow separate measures for top and bottom // fading edge; // in this particular case we would like to disable the top, but not the // bottom edge. if (mHeaderView != null) { // 设置边框渐变的长度 setFadingEdgeLength(0); } // requestLayout(); } public void setAdapter(ListAdapter adapter) { super.setAdapter(adapter); mAdapter = (PinnedHeaderAdapter) adapter; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); // 得到mHeaderViewz的宽、高 if (mHeaderView != null) { measureChild(mHeaderView, widthMeasureSpec, heightMeasureSpec); mHeaderViewWidth = mHeaderView.getMeasuredWidth(); mHeaderViewHeight = mHeaderView.getMeasuredHeight(); } } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); if (mHeaderView != null) { mHeaderView.layout(0, 0, mHeaderViewWidth, mHeaderViewHeight); configureHeaderView(getFirstVisiblePosition()); } } public void configureHeaderView(int position) { if (mHeaderView == null) { return; } int state = mAdapter.getPinnedHeaderState(position); switch (state) { case PinnedHeaderAdapter.PINNED_HEADER_GONE: { // mHeaderViewVisible = false; break; } case PinnedHeaderAdapter.PINNED_HEADER_VISIBLE: { mAdapter.configurePinnedHeader(mHeaderView, position); // if (mHeaderView.getTop() != 0) { // mHeaderView.layout(0, 0, mHeaderViewWidth, mHeaderViewHeight); // } // mHeaderViewVisible = true; break; } case PinnedHeaderAdapter.PINNED_HEADER_PUSHED_UP: { View firstView = getChildAt(0); int bottom = firstView.getBottom(); int headerHeight = mHeaderView.getHeight(); int y; if (bottom < headerHeight) { y = (bottom - headerHeight); } else { y = 0; } mAdapter.configurePinnedHeader(mHeaderView, position); if (mHeaderView.getTop() != y) { mHeaderView.layout(0, y, mHeaderViewWidth, mHeaderViewHeight + y); } mHeaderViewVisible = true; break; } } } @Override protected void dispatchDraw(Canvas canvas) { super.dispatchDraw(canvas); if (mHeaderViewVisible) { drawChild(canvas, mHeaderView, getDrawingTime()); } } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="fill_parent" android:orientation="vertical" > <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="top" android:background="@drawable/topbar2" android:text=" U - Job" android:textColor="#FFFFFF" android:textSize="40dp" android:id="@+id/topBar" /> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:layout_width="45dp" android:layout_height="45dp" android:layout_alignParentRight="true" android:layout_alignTop="@id/topBar" android:background="@drawable/refresh" /> </RelativeLayout> </FrameLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/topbar2" android:text="header" android:textColor="#FFFFFF" android:textSize="40dp" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="header_text" android:textSize="40dp" /> </LinearLayout> <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="header" android:textSize="40dp" android:id="@+id/section_list_view" /> </LinearLayout>
section_list_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/topbar2" android:text="header" android:textColor="#FFFFFF" android:textSize="40dp" android:id="@+id/header" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="header_text" android:textSize="40dp" android:id="@+id/example_text_view" /> </LinearLayout>
亲,你明白了吗?
<ListView |
73 | android:layout_width="fill_parent" |
74 | android:layout_height="wrap_content" |
75 | android:text="header" |
76 | android:textSize="40dp" |
77 | android:id="@+id/section_list_view" |
78 | /> |
<lzy.app.jobSystem.PinnedHeaderListView |
73 | android:layout_width="fill_parent" |
74 | android:layout_height="wrap_content" |
75 | android:text="header" |
76 | android:textSize="40dp" |
77 | android:id="@+id/section_list_view" |
78 | /> |
main.xml里面listview应该改成你自定义的吧
######非常感谢,以前没见过添加自定义的标签,弄了一天原来要加包名才能实现######亲,只有PinnedHeaderListView 可以转为listView ,listView不可以转为PinnedHeaderListView 的。
@supperman 正解
亲,只有PinnedHeaderListView 可以转为listView ,listView不可以转为PinnedHeaderListView 的。
@supperman 正解
这是什么 lzy.app.jobSystem. PinnedHeaderListView包名,lzy=楼主呀
######haha,对啊,自己的项目包名版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。