在动作条福尔摩斯,添加标签掷:提交已经叫 [英] in Actionbar sherlock, add tab throw: commit already called

查看:100
本文介绍了在动作条福尔摩斯,添加标签掷:提交已经叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用ActionBarSherlock在android的选项卡视图。它只有3个选项卡的示范项目,每个标签只显示其中包含一个TextView片段。这是我的 MainActivity code:

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

}

在code以上会导致我的申请强制关闭已经提交名为错误。我搜索了很多教程,但没有任何线索。谁能解释并解决一下吗?非常感谢你!

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!

推荐答案

我知道这是一个老问题,但由于没有给出答复,在这儿呢。变化:

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

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

该FragmentTransaction将自动获得COMMITED,从而明确地调用.commit()将抛出你遇到的错误。

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

这篇关于在动作条福尔摩斯,添加标签掷:提交已经叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆