2 yx19961220 yx19961220 于 2016.04.28 13:05 提问

菜鸟请大神指教,关于在fragment中加入vierpager的问题

package com.yanis.yc_ui_fragment_menu;

import java.util.ArrayList;
import java.util.List;

import com.health.Circle;
import com.health.Information;
import com.health.Market;

import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentCategory extends Fragment{

//声明viewpager
private ViewPager viewPager;
//声明数据源
private List<Fragment> list;
//声明适配器
private FragmentStatePagerAdapter adapter;
//声明view
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.fragment_category, container, false);
    init();
    return view;
}

//初始化控件的方法
private void init() {
    // TODO Auto-generated method stub
    //得到viewpager
    viewPager=(ViewPager) view.findViewById(R.id.viewpager_tab);
    //得到列表
    list=new ArrayList<Fragment>();
    //往列表里添加fragment
    Information information=new Information();
    Market market=new Market();
    Circle circle=new Circle();
    list.add(information);
    list.add(market);
    list.add(circle);
    //声明适配器
    adapter=new FragmentStatePagerAdapter(getChildFragmentManager()) {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public Fragment getItem(int arg0) {
            // TODO Auto-generated method stub
            return list.get(arg0);
        }

        @Override
        public Object instantiateItem(ViewGroup arg0, int arg1) {
            // TODO Auto-generated method stub
            return super.instantiateItem(arg0, arg1);
        }

        @Override
        public void destroyItem(ViewGroup container, int position,
                Object object) {
            // TODO Auto-generated method stub
            super.destroyItem(container, position, object);
        }
    };

    viewPager.setAdapter(adapter);
}

}

4个回答

yx19961220
yx19961220   2016.04.28 13:08

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yanis.yc_ui_fragment_menu/com.yanis.yc_ui_fragment_menu.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class fragment

yx19961220
yx19961220   2016.04.28 13:08

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yanis.yc_ui_fragment_menu/com.yanis.yc_ui_fragment_menu.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class fragment

yx19961220
yx19961220   2016.04.28 13:08

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yanis.yc_ui_fragment_menu/com.yanis.yc_ui_fragment_menu.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class fragment

yx19961220
yx19961220   2016.04.28 13:09

百分不得其解,已经导了getChildFragmentManager 为什么还会报错,请大神们指点

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!