weixin_39632524
weixin_39632524
2021-01-07 04:55

FragmentPagerAdapter-java.lang.IllegalStateException: Fragment already added

Hi Jeremy Feinstein, I am trying to use your JazzyViewPager with a FragmentPagerAdapter,but it occur a
java.lang.IllegalStateException: Fragment already added: HomeFragment{40f97560 #0 id=0x7f040012 android:switcher:2130968594:0}

here is my code:

final class FMPagerAdapter extends FragmentPagerAdapter {


    private List<fragment> fragments;

    public FMPagerAdapter(FragmentManager fm, List<fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }


    public boolean isViewFromObject(View view, Object object) {
        if(object != null){
            return ((Fragment)object).getView() == view;
        }else{
            return false;
        }
    }


    public Object instantiateItem(ViewGroup container, int position) {
        Object obj = super.instantiateItem(container, position);
        viewPager.setObjectForPosition(obj, position);
        return super.instantiateItem(container, position);
    }



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



    public int getCount() {
        // TODO Auto-generated method stub
        return this.fragments.size();
    }

}
</fragment></fragment>

and if i don't overwrite instantiateItem method ,it works well ,but have no TransitionEffect..

该提问来源于开源项目:jfeinstein10/JazzyViewPager

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • weixin_39632524 weixin_39632524 3月前

    I am so sorry, It's my careless...

    in instantiateItem must return obj.

    public Object instantiateItem(ViewGroup container, int position) {
        Object obj = super.instantiateItem(container, position);
        viewPager.setObjectForPosition(obj, position);
       // return super.instantiateItem(container, position);
        return obj;
    }
    
    点赞 评论 复制链接分享
  • weixin_39792049 weixin_39792049 3月前

    Hi, i am having same issue,my pageadapter class is

    public class MyPagerAdapter extends FragmentPagerAdapter {

    
    private List<fragment> fragments;
    private JazzyViewPager mJazzy;
    
    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
        this.fragments = new ArrayList<fragment>();
        fragments.add(new Fragmentone());
        fragments.add(new Fragmenttwo());
        fragments.add(new Fragmentthree());
    }
    
    
    public Fragment getItem(int position) {
        return fragments.get(position);
    }
    
    
    public int getCount() {
        return fragments.size();
    }
    public Object instantiateItem(ViewGroup container, final int position) {
        Object obj = super.instantiateItem(container, position);
        mJazzy.setObjectForPosition(obj, position);
        return obj;
        }
    
    public boolean isViewFromObject(View view, Object object) {
        if(object != null){
            return ((Fragment)object).getView() == view;
        }else{
            return false;
        }
    }
    </fragment></fragment>

    }

    and i am calling it as:

    vpage = (JazzyViewPager) findViewById(R.id.jazzy_pager); vpage.setTransitionEffect(effect); MyPagerAdapter pageAdapter = new MyPagerAdapter(getSupportFragmentManager()); vpage.setAdapter(pageAdapter);

    But i am getting following error: can u please please help me...

    10-16 22:02:56.571: E/AndroidRuntime(29181): FATAL EXCEPTION: main 10-16 22:02:56.571: E/AndroidRuntime(29181): java.lang.NullPointerException 10-16 22:02:56.571: E/AndroidRuntime(29181): at pl.looksok.viewpagerdemo.MyPagerAdapter.instantiateItem(MyPagerAdapter.java:38) 10-16 22:02:56.571: E/AndroidRuntime(29181): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832) 10-16 22:02:56.571: E/AndroidRuntime(29181): at android.support.v4.view.ViewPager.populate(ViewPager.java:982) 10-16 22:02:56.571: E/AndroidRuntime(29181): at android.support.v4.view.ViewPager.populate(ViewPager.java:914) 10-16 22:02:56.571: E/AndroidRuntime(29181): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436) 10-16 22:02:56.571: E/AndroidRuntime(29181): at android.view.View.measure(View.java:12911) 10-16 22:02:56.571: E/AndroidRuntime(29181): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4805) 10-16 22:02:56.571: E/AndroidRuntime(29181): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1399) 10-16 22:02:56.571: E/AndroidRuntime(29181): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1033) 10-16 22:02:56.571: E/AndroidRuntime(29181): at android.widget.LinearLayout.onMeasure(LinearLayout.java:559) 10-16 22:02:56.571: E/AndroidRuntime(29181): at android.view.View.measure(View.java:12911) 10-16 22:02:56.571: E/AndroidRuntime(29181): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4805) 10-16 22:02:56.571: E/AndroidRuntime(29181): at android.widget.FrameLayout.onMeasure(FrameLayout.java:297) 10-16 22:02:56.571: E/AndroidRuntime(29181): at android.view.View.measure(View.java:12911) 10-16 22:02:56.571: E/AndroidRuntime(29181): at android.widget.LinearLayout.measureVertical(LinearLayout.java:828) 10-16 22:02:56.571: E/AndroidRuntime(29181): at android.widget.LinearLayout.onMeasure(LinearLayout.java:557) 10-16 22:02:56.571: E/AndroidRuntime(29181): at android.view.View.measure(View.java:12911) 10-16 22:02:56.571: E/AndroidRuntime(29181): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4805) 10-16 22:02:56.571: E/AndroidRuntime(29181): at android.widget.FrameLayout.onMeasure(FrameLayout.java:297) 10-16 22:02:56.571: E/AndroidRuntime(29181): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2097) 10-16 22:02:56.571: E/AndroidRuntime(29181): at android.view.View.measure(View.java:12911) 10-16 22:02:56.571: E/AndroidRuntime(29181): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064) 10-16 22:02:56.571: E/AndroidRuntime(29181): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2446) 10-16 22:02:56.571: E/AndroidRuntime(29181): at android.os.Handler.dispatchMessage(Handler.java:99) 10-16 22:02:56.571: E/AndroidRuntime(29181): at android.os.Looper.loop(Looper.java:137) 10-16 22:02:56.571: E/AndroidRuntime(29181): at android.app.ActivityThread.main(ActivityThread.java:4448) 10-16 22:02:56.571: E/AndroidRuntime(29181): at java.lang.reflect.Method.invokeNative(Native Method) 10-16 22:02:56.571: E/AndroidRuntime(29181): at java.lang.reflect.Method.invoke(Method.java:511) 10-16 22:02:56.571: E/AndroidRuntime(29181): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823) 10-16 22:02:56.571: E/AndroidRuntime(29181): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590) 10-16 22:02:56.571: E/AndroidRuntime(29181): at dalvik.system.NativeStart.main(Native Method)

    点赞 评论 复制链接分享
  • weixin_39866487 weixin_39866487 3月前

    Try change FragmentPagerAdapter with FragmentStatePagerAdapter

    source https://github.com/jfeinstein10/JazzyViewPager/issues/46

    点赞 评论 复制链接分享
  • weixin_39821035 weixin_39821035 3月前

    FragmentStatePageradapter it didn't work

    点赞 评论 复制链接分享