当我引用Android Developers链接来制作下拉列表时,我在Eclipse中遇到错误:
The method openFragmentTransaction() is undefined for the type new ActionBar.OnNavigationListener(){}
这是网站给出的示例代码段(迄今为止):
mOnNavigationListener = new OnNavigationListener() {
String[] strings = getResources().getStringArray(R.array.action_list);
@Override
public boolean onNavigationItemSelected(int position, long itemId) {
ListContentFragment newFragment = new ListContentFragment();
FragmentTransaction ft = openFragmentTransaction(); //<--ERROR HERE
ft.replace(R.id.fragment_container, newFragment, strings[position]);
// Apply changes
ft.commit();
return true;
}
};
问题是,我不知道这种方法来自哪里?我的主要活动类是SherlockFragmentActivity
,我从android.support.v4.app.FragmentTransaction
导入。按“ctrl + shift + o”将无济于事。有人可以给出解决方案的指针吗?
答案 0 :(得分:3)
我想我找到了答案。只需改变
FragmentTransaction ft = openFragmentTransaction();
到
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();