开发者社区> 问答> 正文

强转成自己写的listview时报ClassCaseException:报错

自己写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());  
        }  
    }

}</pre> 

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>

展开
收起
kun坤 2020-06-10 09:28:22 593 0
1 条回答
写回答
取消 提交回答
  • 亲,你明白了吗?

      <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 正解 ######谢谢哈哈,原来不能用这样方法拿出来,后来我用了代码动态插入那个ListView就好了。######

    引用来自“jacky2058”的评论

    亲,只有 PinnedHeaderListView 可以转为listView ,listView不可以转为PinnedHeaderListView 的。 @supperman 正解 目测楼主小白一只~######谢谢哈哈,原来不能用这样方法拿出来,后来我用了代码动态插入那个ListView就好了######这是什么 lzy.app.jobSystem. PinnedHeaderListView包名,lzy=楼主呀######haha,对啊,自己的项目包名
    2020-06-10 09:28:30
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载