JakeWharton/ActionBarSherlock — pull request #402 by gabrielittner (2012-03-23 16:32:37+00:00)
Title: fixes NPE ActionBar.TabListener

This is a fix for Issue #391. The explanation is in the commit message.

For testing you can overwrite the FragmentTabs class in the fragment sample with the following code:

public class FragmentTabs extends SherlockFragmentActivity {

ActionBar actionBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(SampleList.THEME); //Used for theme switching in samples
super.onCreate(savedInstanceState);

actionBar = getSupportActionBar();
actionBar.addTab(actionBar.newTab().setText("Simple")
.setTabListener(new TabListener
(this, "simple", FragmentStackSupport.CountingFragment.class)));
actionBar.addTab(actionBar.newTab().setText("Contacts")
.setTabListener(new TabListener
(this, "contacts", LoaderCursorSupport.CursorLoaderListFragment.class)));
actionBar.addTab(actionBar.newTab().setText("Custom")
.setTabListener(new TabListener
(this, "custom", LoaderCustomSupport.AppListFragment.class)));
actionBar.addTab(actionBar.newTab().setText("Throttle")
.setTabListener(new TabListener
(this, "throttle", LoaderThrottleSupport.ThrottledLoaderListFragment.class)));
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}

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

public TabListener(Activity activity, String tag, Class clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}

public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (ft == null) {
Log.i("tabselected", "fragmenttransaction == null");
return;
}
if (mFragment == null) {
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
} else {
ft.attach(mFragment);
}
}

public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (ft == null) {
Log.i("tabunselected", "fragmenttransaction == null");
return;
}
if (mFragment != null) {
ft.detach(mFragment);
}
}

public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
}