开发者社区> 问答> 正文

安卓listview 操作?报错

list= new ArrayList<String>();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        
        //setContentView(R.layout.list);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
        footer = View.inflate(this,R.layout.active,null);
    
    
        activeUser = (ListView) findViewById(R.id.activeUser);//list 活动列表
        
    
          
        /**
         * 第一次添加数据
         */
        for (int i = 0; i < 10; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("PIC", "Test Title");
            
            listItem.add(map);
        }
        listItemAdapter = new SimpleAdapter(this,
                listItem, R.layout.list,
                new String[] { "PIC" }, new int[] { R.id.textView});
        
        
        
        /**
         * 注册事件
         */
    //    activeUser.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,200));
        
        activeUser.setOnScrollListener(new OnScrollListener() {
               
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            
            }
             Thread t =null;
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                // TODO Auto-generated method stub
                
                int last = activeUser.getLastVisiblePosition();
                
                if((last+1)==totalItemCount){
                    activeUser.addFooterView(view, null, false);
                    isLoad = false;
                  t =    new Thread(new Runnable() {
                        
                        @Override
                        public void run() {
                            try {
                                Thread.sleep(3000);
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            ArrayList<HashMap<String, Object>> listItem1 = new ArrayList<HashMap<String, Object>>();
                            for(int i=200;i<220;i++)  
                            {  
                                HashMap<String, Object> map = new HashMap<String, Object>();  
                                
                                map.put("ItemTitle", "Level "+i);  
                                listItem1.add(map);
                                
                            }  
                            //handler.sendMessage(handler.obtainMessage(100,listItem1));
                            
                        }
                    });
                    
                
                    
                    
                    
                    
                }
            }
            Handler handler = new Handler(){
                
                @SuppressWarnings("unchecked")
                @Override
                public void handleMessage(Message msg) {
                    // TODO Auto-generated method stub
                    listItem.addAll((ArrayList)msg.obj);
                    listItemAdapter.notifyDataSetChanged();
                    isLoad =true;
                    
//                    if(activeUser.getFooterViewsCount() >0){
//                        activeUser.removeFooterView(view);
//                    }
                }
            };
        });
        //reloadDateActive = (ProgressBar)findViewById(R.id.refresh_list_header_progressbar);
        
    
        TextView footer1 = new TextView(this);
//        activeUser.addFooterView(footer1);
        /**
         * 数据包装
         */
        
        activeUser.setAdapter(listItemAdapter);  
//        activeUser.removeFooterView(footer1);
        
        reload = (ImageView) findViewById(R.id.reload);
        reload.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                System.out.println("--------------------------------------11111111111111111111111111111111");
                Toast.makeText(MainActivity.this, "重新刷新",1).show();
                
            }
        });
        edit = (ImageView) findViewById(R.id.reload);
        
        edit.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "编辑操作",1).show();
                
            }
        });



请问启动时候java.lang.RuntimeException: Unable to start activity ComponentInfo{com.chat/com.chat.MainActivity}: java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams


activeUser.setAdapter(listItemAdapter); 
执行到这步就报错

展开
收起
爱吃鱼的程序员 2020-06-22 11:27:17 589 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    明显是需要使用android.widget.AbsListView.LayoutParams,而你使用了另外一个LayoutParams,这句不是已经说明了么?
    <preclass="brush:java;toolbar:true;auto-links:false;">android.widget.FrameLayout$LayoutParamscannotbecasttoandroid.widget.AbsListView$LayoutParams



    <divclass="ref">

    引用来自“bigtiger02”的答案

    <divclass=ref_body>明显是需要使用android.widget.AbsListView.LayoutParams,而你使用了另外一个LayoutParams,这句不是已经说明了么?
    <preclass="brush:java;toolbar:true;auto-links:false;">android.widget.FrameLayout$LayoutParamscannotbecasttoandroid.widget.AbsListView$LayoutParams



    2020-06-22 11:27:36
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载