IllegalStateException с ActionBarSherlock 4 - PullRequest
0 голосов
/ 17 марта 2012

У меня этот код прекрасно работал с ActionBarSHerlock 3. Теперь он дает мне IllegalStateException при запуске.

Я думаю, что это связано с заменой фрагмента, но я не могу сказать, в чем проблема.

РЕДАКТИРОВАТЬ: Я вырезал часть TabsAdapter, которая была неиспользуемой и неиспользованной в коде.

import java.util.ArrayList;


import com.viewpagerindicator.TabPageIndicator;
import com.viewpagerindicator.TitlePageIndicator;
import com.viewpagerindicator.TitleProvider;







import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentManager;
//import android.support.v4.app.SupportActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockFragmentActivity;

/**
 * Demonstrates combining the action bar with a ViewPager to implement a tab UI
 * that switches between tabs and also allows the user to perform horizontal
 * flicks to move between the tabs.
 */
public class ActionBarTabsPager extends SherlockFragmentActivity implements SecondFragment.OnButtonClickedXListener{
    ViewPager  mViewPager;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.i("ONCREATE START","ONCREATE START");


        setContentView(R.layout.actionbar_tabs_pager);


        if (savedInstanceState == null) {


            Fragment newFragment = new FirstFragment();
            Fragment newFragment2 = new SecondFragment();

            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.add(R.id.pager, newFragment, "FirstFragment");
                Log.i("FirstFragment Added","FirstFragment Added");
            ft.add(R.id.pager, newFragment2, "SecondFragment");
                Log.i("SecondFragment Added","SecondFragment Added");
            ft.commit();


       }

        //getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        //ActionBar.Tab tab1 = getSupportActionBar().newTab().setText("Fragment 1");
        //ActionBar.Tab tab2 = getSupportActionBar().newTab().setText("Fragment 2");


        mViewPager = (ViewPager)findViewById(R.id.pager);
        //mViewPager.setAdapter(new TabsAdapter(this, getSupportActionBar(), mViewPager));
        mViewPager.setAdapter(new VPIFragmentAdapter(getSupportFragmentManager()));


      //Bind the title indicator to the adapter
        TitlePageIndicator titleIndicator = (TitlePageIndicator)findViewById(R.id.titles);
        titleIndicator.setViewPager(mViewPager);



        //mTabsAdapter = new TabsAdapter(this, getSupportActionBar(), mViewPager);

            //mTabsAdapter.addTab(tab1, FirstFragment.class);
            //mTabsAdapter.addTab(tab2, SecondFragment.class);//LoaderCursorSupport.CursorLoaderListFragment.class);



        if (savedInstanceState != null) {

            //getSupportActionBar().setSelectedNavigationItem(savedInstanceState.getInt("index"));
        }

        Log.i("ONCREATE END","ONCREATE END");

    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onRestoreInstanceState(savedInstanceState);

        //Ripristiniamo lo stato del contento dei fragment nel caso di cambio di activity o di orientamento
        FirstFragment ff = (FirstFragment) getSupportFragmentManager().findFragmentByTag("FirstFragment");
        View root = ff.getView();
        TextView tv = (TextView) root.findViewById(R.id.textView1);
        tv.setText(savedInstanceState.getString("textView1content"));


    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        Log.i("onSaveInstanceState START","onSaveInstanceState START");

        super.onSaveInstanceState(outState);

        outState.putInt("index", getActionBar().getSelectedNavigationIndex());

        //Salvo il contenuto della stringa nel FirstFragment
        FirstFragment ff = (FirstFragment) getSupportFragmentManager().findFragmentByTag("FirstFragment");
        View root = ff.getView();
        TextView tv = (TextView) root.findViewById(R.id.textView1);

        outState.putString("textView1content", (String)tv.getText());

        Log.i("onSaveInstanceState END","onSaveInstanceState END");

    }




    public class VPIFragmentAdapter extends FragmentPagerAdapter implements TitleProvider {

        public VPIFragmentAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }

        private FragmentTransaction mCurTransaction = null;


        /*public VPIFragmentAdapter(android.support.v4.app.FragmentManager fm) {
            super(fm);
        }*/


        @Override
        public Object instantiateItem(View container, int position) {
            if (mCurTransaction == null) {
                mCurTransaction = getSupportFragmentManager().beginTransaction();
            }
            // TODO Auto-generated method stub

            Fragment fragment = getItem(position);


            if (fragment!=null){
                Log.i("Fragment Found!","Fragment Found! "+fragment.getTag());
                mCurTransaction.attach(fragment);
                mCurTransaction.commit();
                }


            return fragment;//super.instantiateItem(container, position);
        }


        @Override
        public Fragment getItem(int position) {
            Log.i("VPI getItem","getItem");
            Fragment f;

            if (position==0)
                {Log.i("VPI position=0","position=0");
                f=getSupportFragmentManager().findFragmentByTag("FirstFragment");
                Log.i("FOUND FRAGMENT!",""+f.getTag());
                return f;}

            else if (position==1)
                {Log.i("VPI position=1","position=1");
                f=getSupportFragmentManager().findFragmentByTag("SecondFragment");
                Log.i("FOUND FRAGMENT!",""+f.getTag());
                return f;}

            else return null;//Fragment.instantiate(mContext, mTabs.get(position), null);

        }


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

        @Override
        public String getTitle(int position) {
            if (position==0) return "First";
            else if (position==1) return "Second";
            // TODO Auto-generated method stub
            else return null;
        }


    }


    @Override
    public void OnButtonClickedX(View v) {

        if (v==findViewById(R.id.button1)){
            Log.i("TRIGGERED","TRIGGERED");

            FirstFragment ff = (FirstFragment) getSupportFragmentManager().findFragmentByTag("FirstFragment");

            View root = ff.getView();
            TextView tv = (TextView) root.findViewById(R.id.textView1);
            Log.i("testo prima della modifica",""+tv.getText());
            tv.setText("MODIFICATO");

            Log.i("testo dopo la modifica",""+tv.getText());
        }
        // TODO Auto-generated method stub

    }







}

ЗДЕСЬ ЖУРНАЛ

FATAL EXCEPTION: main
 java.lang.IllegalStateException: Can't change tag of fragment FirstFragment{40545228 #0 id=0x7f040028 FirstFragment}: was FirstFragment now android:switcher:2130968616:0
    at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:351)
    at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:342)
    at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:97)
    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:649)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:783)
    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1016)
    at android.view.View.measure(View.java:8366)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
    at android.view.View.measure(View.java:8366)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
    at android.view.View.measure(View.java:8366)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
    at android.view.View.measure(View.java:8366)
    at android.view.ViewRoot.performTraversals(ViewRoot.java:847)
    at android.view.ViewRoot.handleMessage(ViewRoot.java:1868)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3691)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
    at dalvik.system.NativeStart.main(Native Method)
...