空FragmentTransaction被传递给TabListener.onTabSelected()FragmentTransaction、TabListener、onTabSelected

2023-09-04 10:03:03 作者:永逺有多逺

我使用的是抽样code此处提供添加导航选项卡的操作栏: http://developer.android.com/guide/topics/ui/actionbar.html我使用ActionBarSherlock。

I'm adding navigation tabs to an action bar using the sample code provided here: http://developer.android.com/guide/topics/ui/actionbar.html I'm using ActionBarSherlock.

我的TabListener直接从文档复制。

My TabListener is copied directly from the docs.

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

和我在活动设立了监听器:

And I set up the listener in my activity:

    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()的监听器。我应该建立比较亲切?

But the app blows up with a null pointer exception because a null FragmentTransaction is being passed to onTabSelected() in the listener. Should I be creating one somewhere?

我已经一遍又一遍的文档,我pretty的困惑。你可以看到我已经错过了?

I've been over and over the docs, and I'm pretty confused. Can you see what I've missed?

推荐答案

这似乎是一个问题兼容性库(因此通过关联ActionBarSherlock)。解决的办法是忽略传入的(空)FragmentTransaction,并获得你自己的。下面是马克·墨菲的一个例子:

This seems to be an issue with the compatibility library (and therefore by association ActionBarSherlock). The solution is to ignore the (null) FragmentTransaction that is passed in, and get your own. Here's an example from 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

 
精彩推荐