xiaojiangren 2016-02-18 12:07 采纳率: 27%
浏览 1672
已采纳

关于listvi中加入viewpager 求大神看下代码

我是写了一个viewpager 有三个页面 然后在第一个页面中加入了一个listview 然后在listview中加入一个viewpager 但是每次运行都报错

java.lang.ClassCastException: android.support.v4.view.ViewPager$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

下面是最外层viewpager的第一个页面中的代码

public class FragmenrOne extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.listview, container, false); //这个是自己写的listview
list = (List) view.findViewById(R.id.list);
View view2 = inflater.inflate(R.layout.otherfragment, container, false); //这个是viewpager
viewpager = (ViewPager) view2.findViewById(R.id.otherpager);
madapter=new OtherAdapter(getChildFragmentManager()); //viewpager的Adapter
viewpager.setAdapter(madapter);
list.addHeaderView(view2);

return view;
}

}

当我把listview的代码去掉 或者把viewpager的代码去掉 都可以正常显示出来 就是把viewpager加到listview中后就报错 这是什么原因啊 一直没法解决啊

  • 写回答

5条回答 默认 最新

  • xiaojiangren 2016-02-19 08:13
    关注

    解决了
    把View view2 = inflater.inflate(R.layout.otherfragment, container, false);
    换成View headerVeiew = LayoutInflater.from(getActivity()).inflate(R.layout.otherfragment, null);
    其它不变 就可以正常运行了 至于什么原因我也还不太清楚 知道的大神麻烦告诉一下!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • Exploring1024 2016-02-18 12:43
    关注

    错误提示是二者之间的LayoutParams使用有问题,这里面看不出来

    评论
  • threenewbee 2016-02-18 12:51
    关注
    评论
  • keyborad_boy 2016-02-19 01:53
    关注

    list = (List) view.findViewById(R.id.list);这个有问题

    评论
  • qq_27578023 2016-06-08 14:47
    关注

    把View view2 = inflater.inflate(R.layout.otherfragment, container, false);
    换成View headerVeiew = LayoutInflater.from(getActivity()).inflate(R.layout.otherfragment, null);

    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 电脑锁屏时vba运行SAP,excel数据不能复制到SAP
  • ¥50 74LS系列 74LS00 74LS04设计一个RS485电路(关键词-差分)
  • ¥30 各位help写一下代码
  • ¥15 在运行SDEdit模型下载不了
  • ¥15 求51控制l298n驱动的小车中超声波避障怎么写
  • ¥15 电脑连上WIFI却用不了
  • ¥30 MATLAB在RLC电路的固有响应和阶跃响应GUI仿真报告
  • ¥15 hyper-v出现的问题
  • ¥15 有能用的可加酬金,求可以批量下载懒人听书的软件,能登录自己帐号的。
  • ¥100 高博一起做RGB-D SLAM(5)VO无法出visualisation问题