我无法确定是否在导航栏中选择了某个选项卡。我正在尝试确定是否选择了成分选项卡。每次我运行我的代码时,它都会返回一个空指针错误,因为 mFragment 为空且尚未实例化。
好的...我知道 mFragment 为空。我想这就是我感到困惑的地方。在比较片段时,您是在比较它们的内容还是对象本身?如果有人知道如何比较片段,那就太好了。先感谢您。这是代码:
public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
private final Bundle mArgs;
private Bundle mTitleArgs;
private Fragment mFragment;
private IngredientCreation ingredientFrag;
private FragmentTabControl controller;
public TabListener(Activity activity, String tag, Class<T> clz) {
this(activity, tag, clz, null);
}
public TabListener(Activity activity, String tag, Class<T> clz, Bundle args) {
mActivity = activity;
mTag = tag;
mClass = clz;
mArgs = args;
// Check to see if we already have a fragment for this tab, probably
// from a previously saved state. If so, deactivate it, because our
// initial state is that a tab isn't shown.
mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
if (mFragment != null && !mFragment.isDetached()) {
FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction();
ft.detach(mFragment);
ft.commit();
}
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ingredientFrag = (IngredientCreation) mActivity.getFragmentManager().findFragmentByTag("ingredients");
if (mFragment.equals(ingredientFrag)) {
System.out.println("got here");
mFragment = Fragment.instantiate(mActivity, mClass.getName(), mTitleArgs);
ft.add(android.R.id.content, mFragment, mTag);
} else {
ft.attach(mFragment);
}
}