- findViewById()函数
程序运行的时候在 Banner banner = view.findViewById(R.id.banner_adver); 这一句报了空指针异常的error。一般用findViewById()都是在Activity的OnCreate()方法里面,而在Fragment中使用findViewById()时候需要创建Fragment的View对象,然后调用View对象的findViewById()方法
public View view;
view = inflater.inflate(R.layout.fragment_home, container, false);
private void initBanner() {
ArrayList<Integer> imagPath = new ArrayList<>();
//获取资源id
Banner banner = view.findViewById(R.id.banner_adver);
- SimpleAdapter()
SimpleAdapter(Context context, data, int resource, String[] from, int[] to);
第一个参数是上下文,一般在Activity中context使用this指针,但是在Fragment中会报错。
改成这样写就可以了:
adapter=new SimpleAdapter(getActivity(), dataList, R.layout.item_channel, from, to);
因为getActivity()可以获取活动页面的上下文
- Intent()
和上一个一样的原因,原本this指针要用getActivity()获取活动页面的上下文
之前用Viewpager+Fragment实现页面切换,结果因为Fragment的问题进度停了好几天… 还是太菜了啊
感觉改bug这种事一定要有耐心有信心( •᷄⌓•᷅ )੨੨ 之前觉得改不出的时候看一天也不知道错在哪儿,觉得改的出的时候一两小时就改完了