传递给TabListener.onTabSelected()的Null FragmentTransaction

时间:2011-12-27 14:12:37

标签: android

我正在使用此处提供的示例代码向操作栏添加导航标签: http://developer.android.com/guide/topics/ui/actionbar.html我正在使用ActionBarSherlock。

我的TabListener直接从文档中复制。

    public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
    private Fragment mFragment;
    private final Activity mActivity;
    private final String mTag;
    private final Class<T> mClass;

    /** Constructor used each time a new tab is created.
      * @param activity  The host Activity, used to instantiate the fragment
      * @param tag  The identifier tag for the fragment
      * @param clz  The fragment's Class, used to instantiate the fragment
      */
    public TabListener(Activity activity, String tag, Class<T> clz) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
    }

    /* The following are each of the ActionBar.TabListener callbacks */

    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        // Check if the fragment is already initialized
        if (mFragment == null) {
            // If not, instantiate and add it to the activity
            mFragment = Fragment.instantiate(mActivity, mClass.getName());
            ft.add(android.R.id.content, mFragment, mTag);
        } else {
            // If it exists, simply attach it in order to show it
            ft.attach(mFragment);
        }
    }

    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
            // Detach the fragment, because another one is being attached
            ft.detach(mFragment);
        }
    }

    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
        // User selected the already selected tab. Usually do nothing.
    }
}

我在我的活动中设置了听众:

    ActionBar.Tab tab = actionBar.newTab();
    tab.setText(getString(R.string.TAB_CALC));
    tab.setTabListener(new TabListener<StrokeSelectorFragment>(this, "blah", StrokeSelectorFragment.class));
    actionBar.addTab(tab);

但是应用程序因空指针异常而爆炸,因为将一个空的FragmentTransaction传递给侦听器中的onTabSelected()。我应该在某个地方创建一个吗?

我一直在浏览文档,我很困惑。你能看到我错过的东西吗?

4 个答案:

答案 0 :(得分:16)

我发现Ollie的帖子有用,但不够详细。我设法重构了ActionBar Guide中提供的代码,如下所示(注意:您必须对TabListener中实现的其他方法进行相同的更改):

public void onTabSelected(Tab tab, FragmentTransaction ignoredFt) {
    FragmentManager fragMgr = ((FragmentActivity)mActivity).getSupportFragmentManager();
    FragmentTransaction ft = fragMgr.beginTransaction();

    // Check if the fragment is already initialized
    if (mFragment == null) {
        // If not, instantiate and add it to the activity
        mFragment = Fragment.instantiate(mActivity, mClass.getName());

        ft.add(android.R.id.content, mFragment, mTag);
    } else {
        // If it exists, simply attach it in order to show it
        ft.attach(mFragment);
    }
}

答案 1 :(得分:15)

这似乎是兼容性库的问题(因此通过关联ActionBarSherlock)。解决方案是忽略传入的(null)FragmentTransaction,并获得自己的。以下是Mark Murphy的一个例子:

        FragmentManager fragMgr = getSupportFragmentManager();
        FragmentTransaction ft = fragMgr.beginTransaction();
        ft.commit();

https://groups.google.com/forum/#!msg/android-developers/pCnSx7sTIZ8/cTt1L91M2NgJ

https://groups.google.com/group/actionbarsherlock/browse_thread/thread/89eac58c13fe1ae0

答案 2 :(得分:15)

我在这里找到了解决方案:https://github.com/JakeWharton/ActionBarSherlock/issues/482

仔细检查您的主机活动是否来自SherlockFragmentActivity

答案 3 :(得分:1)

这是ActionBarSherlock的错误。由JakeWharton在版本4.0.1(2012-03-25)中修复。

现在ActionBarSherlock工作正常。