java.lang.IllegalStateException: Can't change tag of fragment

Hi i am getting this error while using a pageViwer and Adapter to slide across 3 fragments.

here is my pageAdapter

  • How to play .mp4 video in videoview in android?
  • Android softkeyboard error message
  • How to play radio live stream .asx video/x-ms-asf?
  • good resource for android admob tutorial
  • The constructor Intent(new View.OnClickListener(){}, Class<DrinksTwitter>) is undefined
  • Android In-App Billing v3: Not receiving signatures
  • public class FreedomPageAdapter extends FragmentPagerAdapter {
    
        private List<Fragment> listFragment;
    
    
        public FreedomPageAdapter(FragmentManager fm, List<Fragment> listFragment) {
    
            super(fm);
            this.listFragment = listFragment;
        }
    
        /*
         * (non-Javadoc)
         * 
         * @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
         */
        @Override
        public Fragment getItem(int position) {
            // TODO Auto-generated method stub
            return listFragment.get(position);
        }
    
        /*
         * (non-Javadoc)
         * 
         * @see android.support.v4.view.PagerAdapter#getCount()
         */
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return listFragment.size();
        }
    

    here is how i define the pageview

    viewPager = (ViewPager) findViewById(R.id.pager);
    
            viewPager.setOnPageChangeListener(this);
    
            savedListFragment = new SubscribedFragment();
    
            fragmentList.add(savedListFragment);
    
            fragmentList.add(savedListFragment);
    
            fragmentList.add(savedListFragment);
    
            viewPager.setAdapter(new FreedomPageAdapter(fragmentManager(),
                    fragmentList));
    
    
    
        private FragmentManager fragmentManager() {
            return getSupportFragmentManager();
        }
    
        private FragmentTransaction getFragmentTransaction() {
            return fragmentManager().beginTransaction();
        }
    

    page view listener

    /*
         * (non-Javadoc)
         * 
         * @see android.support.v4.view.ViewPager.OnPageChangeListener#
         * onPageScrollStateChanged(int)
         */
        @Override
        public void onPageScrollStateChanged(int arg0) {
    
        }
    
        /*
         * (non-Javadoc)
         * 
         * @see
         * android.support.v4.view.ViewPager.OnPageChangeListener#onPageScrolled
         * (int, float, int)
         */
        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
    
        }
    
        /*
         * (non-Javadoc)
         * 
         * @see
         * android.support.v4.view.ViewPager.OnPageChangeListener#onPageSelected
         * (int)
         */
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
    
        }
    

    Custom fragment object

       public class SubscribedFragment extends Fragment{
    
        /* (non-Javadoc)
         * @see android.app.Fragment#onCreate(android.os.Bundle)
         */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
    
        }
    
        /* (non-Javadoc)
         * @see android.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
         */
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            return inflater.inflate(R.layout.discussion_list_layout, container);
        }
    
    }
    

    Full stack trace of error:

        05-02 19:15:44.599: E/AndroidRuntime(19839): FATAL EXCEPTION: main
    05-02 19:15:44.599: E/AndroidRuntime(19839): java.lang.IllegalStateException: Can't change tag of fragment SubscribedFragment{41157420 id=0x7f070005 android:switcher:2131165189:0}: was android:switcher:2131165189:0 now android:switcher:2131165189:1
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:398)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:389)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:800)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.support.v4.view.ViewPager.populate(ViewPager.java:991)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.support.v4.view.ViewPager.populate(ViewPager.java:880)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1374)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.View.measure(View.java:15518)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.View.measure(View.java:15518)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.View.measure(View.java:15518)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2434)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.View.measure(View.java:15518)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.Choreographer.doFrame(Choreographer.java:532)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.os.Handler.handleCallback(Handler.java:725)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.os.Handler.dispatchMessage(Handler.java:92)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.os.Looper.loop(Looper.java:137)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at android.app.ActivityThread.main(ActivityThread.java:5226)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at java.lang.reflect.Method.invokeNative(Native Method)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at java.lang.reflect.Method.invoke(Method.java:511)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
    05-02 19:15:44.599: E/AndroidRuntime(19839):    at dalvik.system.NativeStart.main(Native Method)
    

  • Android CheckBox — Restoring State After Screen Rotation
  • how to play video from url
  • How to get height of keyboard when activity created in android
  • activity_main cannot be resolved or not a field
  • Pause an Android App with Phonegap
  • Can a website (HTML5,JavaScript) access a mobile device's (android/iPhone) contact list, SDCard files
  • 3 Solutions collect form web for “java.lang.IllegalStateException: Can't change tag of fragment”

    It’s probably because you’re adding the same fragment instance three times to the list. You should create a new instance for each page.

    Also, I suggest looking into FragmentStatePagerAdapter if you’re not too far into development. It may be a better choice if you want to refresh the content of the fragments from the main activity.

    The following method should return one fragment for each tab:

    @Override
    public Fragment getItem(int position) {
        //Add some code
        //to verify that it will not return null
        return listFragment.get(position);
    }
    

    The code is fine, but make sure that the listFragment contains three different Fragment’s, and the fragment is not null.

    You have created an instance of SubscribedFragment() class

    savedListFragment = new SubscribedFragment();
    

    and then you passed the list of this objects into ViewPager Adpater:

    FreedomPageAdapter(FragmentManager fm, List<Fragment> listFragment)
    

    which receives List of Fragment type in its constructor. Since the constructor receives List of Fragment type, try to cast each savedListFragment into Fragment before you add it to the list and then pass it to the adpater’s constructor:

    fragmentList.add((Fragment)savedListFragment);
    viewPager.setAdapter(new FreedomPageAdapter(fragmentManager(), fragmentList));
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.