in Actionbar sherlock, add tab throw: commit alrea

2019-08-29 13:17发布

问题:

I'm trying to use ActionBarSherlock for tab view in android. It's only a demo project with 3 tabs, each tab just show a fragment which contains a textview. This is my MainActivitycode:

public class MainActivity extends SherlockFragmentActivity {

private ActionBar mActionBar;
private Tab tab1;
private Tab tab2;
private Tab tab3;

@Override
protected void onCreate(Bundle arg0) {
    super.onCreate(arg0);
    setContentView(R.layout.activity_main);
    mActionBar = getSupportActionBar();
    mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    tab1 = mActionBar.newTab().setText("TAB1");
    tab2 = mActionBar.newTab().setText("TAB2");
    tab3 = mActionBar.newTab().setText("TAB3");

    tab1.setTabListener(new TabSelectListener(FragmentTest
            .newInstance("Fragment1"), R.id.container));
    tab2.setTabListener(new TabSelectListener(FragmentTest
            .newInstance("Fragment2"), R.id.container));
    tab3.setTabListener(new TabSelectListener(FragmentTest
            .newInstance("Fragment3"), R.id.container));

    mActionBar.addTab(tab1);
    mActionBar.addTab(tab2);
    mActionBar.addTab(tab3);

}

private class TabSelectListener implements TabListener {

    private SherlockFragment mFragment;
    private int resId;

    public TabSelectListener(SherlockFragment fragment, int resId) {
        mFragment = fragment;
        this.resId = resId;
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
    Log.e("Listener", "onTabSelected");
    ft.replace(resId, mFragment).commit();

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        Log.e("Listener", "onTabUnselected");
        // ft.remove(mFragment).commit();
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        Log.e("Listener", "onTabReselected");
    }

}

}

//This is my FragmentTest
public class FragmentTest extends SherlockFragment {
private String name;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment, container, false);
    TextView textView = (TextView) view.findViewById(R.id.tvText);
    textView.setText("" + name);
    return view;
}

public static SherlockFragment newInstance(String s) {
    return new FragmentTest().setName(s);
}

public String getName() {
    return name;
}

public FragmentTest setName(String name) {
    this.name = name;
    return this;
}

}

The code above will cause my application force close with: commit already called error. I searched for many tutorials but no clue. Can anyone explain and fix it for me? Thank you very much!

回答1:

I know this is a old question but since no answer is given, here it is. Change:

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    Log.e("Listener", "onTabSelected");
    ft.replace(resId, mFragment).commit();
}

To

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    Log.e("Listener", "onTabSelected");
    ft.replace(resId, mFragment);
}

The FragmentTransaction will automatically get commited, thus explicitely calling .commit() will throw the error you encounter.