我使用以下代码在Fragment
之间切换,没有任何问题:
private void showSelectedItem(int index) {
Fragment mFragment = null;
if (index == mLastMenuItemSelected)
return;
FragmentTransaction fts = getSupportFragmentManager().beginTransaction();
if (mLastViewFragment != null) {
fts.remove(mLastViewFragment);
}
switch (index) {
case SV.MIS_ShowAllSubjects:
mFragment = new FragmentViewAllSubjects();
fts.replace(R.id.SubjectViewFragment, mFragment);
txtSelectedSlideTitle.setText(UC.getString(R.string.slide_menu_main));
break;
}
fts.commit();
mLastMenuItemSelected = index;
mSlidingMenu.showContent(true);
mLastViewFragment = mFragment;
}
现在我想使用SherlockFragment
代替Fragment
,为了进行此更改,我创建了新的Fragment
:
public class ActionbarTabsPager extends SherlockFragmentActivity {
ViewPager mViewPager;
TabsAdapter mTabsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
}
}
我希望将顶级代码更改为以下代码,以便在SherlockFragment
s:
private void showSelectedItem(int index) {
SherlockFragmentActivity mFragment = null;
if (index == mLastMenuItemSelected)
return;
FragmentTransaction fts = getSupportFragmentManager().beginTransaction();
if (mLastViewFragment != null) {
fts.remove(mLastViewFragment);
}
switch (index) {
case SV.MIS_ShowAllSubjects:
mFragment = new ActionbarTabsPager();
fts.replace(R.id.SubjectViewFragment, mFragment);
txtSelectedSlideTitle.setText(UC.getString(R.string.slide_menu_main));
break;
}
fts.commit();
mLastMenuItemSelected = index;
mSlidingMenu.showContent(true);
mLastViewFragment = mFragment;
}
但我得到了这个错误,我无法解决这个问题:
Error:(141, 29) java: incompatible types: ir.jaziire.Fragments.ActionbarTabsPager cannot be converted to com.actionbarsherlock.app.SherlockListFragment
Error:(149, 29) java: incompatible types: com.actionbarsherlock.app.SherlockListFragment cannot be converted to com.actionbarsherlock.app.SherlockFragmentActivity
我的问题是:
mFragment = new ActionbarTabsPager();
答案 0 :(得分:1)
ActionbarTabsPager
正在延长SherlockFragmentActivity
,而不是SherlockFragment
。变化
public class ActionbarTabsPager extends SherlockFragmentActivity {
到
public class ActionbarTabsPager extends SherlockFragment {
和
SherlockFragmentActivity mFragment = null;
到
Fragment mFragment = null;