传递给TabListener.onTabSelected()的Null FragmentTransaction

我使用此处提供的示例代码将导航选项卡添加到操作栏: 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); 

但应用程序爆炸与空指针exception,因为空的FragmentTransaction被传递给监听器中的onTabSelected()。 我应该在哪里创造一个?

我一直在处理文件,我很困惑。 你能看到我错过了什么吗?

Solutions Collecting From Web of "传递给TabListener.onTabSelected()的Null FragmentTransaction"

我发现奥利的post上面有帮助,但不够详细。 我设法重构ActionBar指南中提供的代码,如下所示(注意:您必须对在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); } } 

这似乎是兼容性库(因此通过关联ActionBarSherlock)的问题。 解决scheme是忽略传入的(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

我在这里find了解决scheme: https : //github.com/JakeWharton/ActionBarSherlock/issues/482

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

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

现在ActionBarSherlock正常工作。