向后滚动时ViewPager NullPointerException

时间:2014-01-09 09:20:55

标签: android nullpointerexception android-viewpager fragmentstatepageradapter

我有一个水平的ViewPager(我们称之为A),每个页面都包含一个片段。每个片段都有一个垂直的ViewPager(我们称之为B),每个页面都包含一个带有一个图像的片段。当我向右滚动A时一切正常,但是当我向左滚动时(我想在viewpager B恢复的那一刻)应用程序崩溃,这是logcat信息:

 01-09 09:55:06.802  25453-25453/com.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
      Process: com.test, PID: 25453
    java.lang.NullPointerException
            at android.support.v4.app.FragmentState.instantiate(Fragment.java:94)
            at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1790)
            at android.support.v4.app.Fragment.performCreate(Fragment.java:1489)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:893)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
            at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
            at android.support.v4.view.ViewPager$3.run(ViewPager.java:244)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
            at android.view.Choreographer.doCallbacks(Choreographer.java:574)
            at android.view.Choreographer.doFrame(Choreographer.java:543)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

水平ViewPager A:

public class ScrollProductsFragment extends Fragment {

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    super.onCreateView(inflater, container, savedInstanceState);

    View view = inflater.inflate(R.layout.fragment_catalogue_scroll, container, false);
    ViewPager mViewPager = (ViewPager) view.findViewById(R.id.pager_details_pager);

    ...

    // load fragments
    ArrayList<Fragment> mListFragments = new ArrayList<Fragment>();
    for (Product product : listProducts) {
        mListFragments.add(ProductFragment.newInstance(product));
    }

    // setup viewpager
    MyStatePagerAdapter mPagerAdapter = new MyStatePagerAdapter(getChildFragmentManager(), mListFragments);
    mViewPager.setAdapter(mPagerAdapter);
    mViewPager.setSaveEnabled(false); 
    mViewPager.setCurrentItem(currentPage);
    mViewPager.setOffscreenPageLimit(2);

    ...
 }

ViewPager A和B使用相同的FragmentStatePagerAdapter

public class MyStatePagerAdapter extends FragmentStatePagerAdapter {

    private ArrayList<Fragment> list;

    public MyStatePagerAdapter(FragmentManager fm, ArrayList<Fragment> list) {
        super(fm);
        this.list = list;
    }

    @Override
    public Fragment getItem(int position) {
        return list.get(position);
    }

    @Override
    public int getItemPosition(Object object){
        return PagerAdapter.POSITION_NONE;
    }

    @Override
    public int getCount() {
        return list.size();
    }
}

ViewPager B:

 public class ProductFragment extends Fragment {

    private List<String> mListImages;
    private DirectionalViewPager mViewPager;

    public static final ProductFragment newInstance(Product product, String imageUrl) {
          ProductFragment fragment = new ProductFragment();
          Bundle bundle = new Bundle();
          bundle.putParcelable(EXTRA_PRODUCT, product);
          fragment.setArguments(bundle);
          return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        super.onCreateView(inflater, container, savedInstanceState);

         this.mProduct = getArguments().getParcelable(EXTRA_PRODUCT);

        View view = inflater.inflate(R.layout.fragment_catalogue_product, container, false);
        mViewPager = (DirectionalViewPager) view.findViewById(R.id.detail_view_pager);

        // set adapter    
        ArrayList<Fragment> imageFragmentList = new ArrayList<Fragment>();    
        for(String image : mProduct.getImages()){
            imageFragmentList.add(ImageFragment.newInstance(image));
        }
        MyStatePagerAdapter pageAdapter = new MyStatePagerAdapter(getChildFragmentManager(),imageFragmentList);    

        // setup viewpager
        mViewPager.setOrientation(DirectionalViewPager.VERTICAL);
        mViewPager.setAdapter(pageAdapter);
        mViewPager.setSaveEnabled(false); 
        mViewPager.setOffscreenPageLimit(2);

        ...

        return view;
    }
}

最后,ImageFragment

public class ImageFragment extends Fragment {

    public static final ImageFragment newInstance(String imageUrl) {
        ImageFragment fragment = new ImageFragment();
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_URL, imageUrl);
        fragment.setArguments(bundle);
        return fragment;
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        super.onCreateView(inflater, container, savedInstanceState);

        String imageUrl = getArguments().getString(EXTRA_URL);

        View view = inflater.inflate(R.layout.fragment_image, container, false);
        ImageView imageView = (ImageView) view.findViewById(R.id.look_image);

        ImageLoader.getInstance().displayImage(imageUrl,imageView);

        return view;
    }

}

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

尝试使用此JakeWharton的库 Android-DirectionalViewPager ,它支持垂直和水平分页以及在运行时在两者之间进行切换。

Gplay Link

enter image description here

致谢:JakeWharton

答案 1 :(得分:0)

通过该logcat信息很难找到问题。你的代码对我来说没问题。也许在FragmentManager中启用调试信息会有所帮助:

getChildFragmentManager().enableDebugLogging(true);

顺便说一句,我猜你在MyStatePagerAdapter类中复制了一个错误的构造函数名称(Horizo​​ntalStatePagerAdapter)。这不应该是问题,因为您将无法编译该代码。 ;)