传递给TabListener.onTabSelected()的Null FragmentTransaction

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

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

public static class TabListener implements ActionBar.TabListener { private Fragment mFragment; private final Activity mActivity; private final String mTag; private final Class 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 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(this, "blah", StrokeSelectorFragment.class)); actionBar.addTab(tab); 

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

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

我发现Ollie的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)。 解决方案是忽略传入的(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了解决方案: https : //github.com/JakeWharton/ActionBarSherlock/issues/482

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

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

现在ActionBarSherlock工作正常。