Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FragmentPagerAdapter-java.lang.IllegalStateException: Fragment already added #34

Open
liuzc opened this issue Sep 9, 2013 · 4 comments

Comments

@liuzc
Copy link

liuzc commented Sep 9, 2013

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;
    }

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

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


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


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

}

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

@liuzc
Copy link
Author

liuzc commented Sep 9, 2013

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

in instantiateItem must return obj.

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

@supsabhi
Copy link

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());
}

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

@Override
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;
    }
@Override
public boolean isViewFromObject(View view, Object object) {
    if(object != null){
        return ((Fragment)object).getView() == view;
    }else{
        return false;
    }
}

}

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)

@saifudin
Copy link

saifudin commented Jul 7, 2015

Try change FragmentPagerAdapter
with
FragmentStatePagerAdapter

source
#46

@StephenGiant
Copy link

FragmentStatePageradapter
it didn't work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants