我无法确定导航栏中是否选择了某个标签。我正在尝试确定是否已选择成分选项卡。每次运行我的代码时,它都会返回一个空指针错误,因为mFragment为null且尚未实例化。
好的......我明白mFragment是null。我想这是我感到困惑的地方。比较片段时,您是在比较它们的内容还是对象本身?如果有人知道如何比较那些很棒的片段。先感谢您。这是代码:
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);
}
}
答案 0 :(得分:0)
ActionBar上的文档提供了一个很好的示例,说明如何正确创建Tab Listener。查看此链接以获得良好的实施。 http://developer.android.com/guide/topics/ui/actionbar.html