在ViewPager中导航时应用程序崩溃

时间:2013-10-28 17:44:57

标签: android android-fragments android-viewpager

我正在使用此实现http://developer.android.com/training/implementing-navigation/lateral.html#horizontal-paging创建一个包含视图分页器和片段的视图库。

在我的代码中,我将从特定登录用户的Web服务器中获取viewpager中的NUM_PAGES(整数)。

我在做什么:

当用户登录到应用程序时,我的页面会填充信息,这会执行许多检查以填充一个页面。所以我在调用onCreate()时创建了所有视图,并在每次片段需要时重用它们。

当我在viewpager中启动多个页面的应用程序时,浏览第1页到第5页,应用程序运行正常,当我尝试重新访问上一页时,应用程序崩溃了“java.lang.IllegalStateException”。 / p>

我是片段和viewpager的新手,请帮我解决这个问题。

提前致谢。

基兰

FATAL EXCEPTION: main
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    at android.view.ViewGroup.addViewInner(ViewGroup.java:3435)
    at android.view.ViewGroup.addView(ViewGroup.java:3306)
    at android.view.ViewGroup.addView(ViewGroup.java:3251)
    at android.view.ViewGroup.addView(ViewGroup.java:3227)
    at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)

我的代码:

protected void onCreate(Bundle savedInstance){

    //my code here

    //prepare views to be used with viewpager
    setupViews();

    //setup viewpager stuff
    mAdapter = new ViewsAdapter(getSupportFragmentManager());

    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);

    setContentView(R.layout.activity_main);

    //other initializations here
}

public static class ViewsAdapter extends FragmentStatePagerAdapter{


    public ViewsAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int position) {
        // TODO Auto-generated method stub
        Fragment fragment = new VehicleFragment();
        Bundle args = new Bundle();

        args.putInt(VehicleFragment.VEHICLE_INDEX, position);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return NUM_PAGES;// this value will be fetched at runtime from a server.
    }

}

public class VehicleFragment extends Fragment {

    public static final String VEHICLE_INDEX = "VEHICLE_INDEX";

    @Override
    public View onCreateView(LayoutInflater inflater,
            ViewGroup container, Bundle savedInstanceState) {
        Bundle args = getArguments();
        int position = args.getInt(VEHICLE_INDEX); 
        Log.i("VehicleHome", "retrieving position : "+position+" in viewsList, where viewsList size = "+viewsList.size());
        return viewsList.get(position);
    }
}

private void setupViews() {

//prepare Views here, the views are created at runtime and added to viewsList (ArrayList) 
//for reuse with fragments. So that each time a new instance of fragment is created we won't endup 
//inflating layout file and initializing the view again and again..

//views logic 

viewsList.add(view);

}

1 个答案:

答案 0 :(得分:0)

我想你不应该在每次请求页面时都创建一个新的片段。而是一次创建所有这些,然后只返回正确的一个:

public static class ViewsAdapter extends FragmentStatePagerAdapter{

    private ArrayList<Fragment> fragments;

    public ViewsAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
        fragments = new ArrayList<Fragment>();
        for (int i = 0; i < NUM_PAGES; i++) {
            Fragment fragment = new VehicleFragment();
            Bundle args = new Bundle();

            args.putInt(VehicleFragment.VEHICLE_INDEX, position);
            fragment.setArguments(args);
            fragments.add(fragment);
        }
    }

    @Override
    public Fragment getItem(int position) {
        // in case dynamic amount of fragments is needed, you can create
        // new ones here if necessary (checking e.g. if position > fragments.size()-1)
        fragments.get(position);
    }

    @Override
    public int getCount() {
        return NUM_PAGES;
    }

}