我正在使用以下代码在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
之间切换: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();
最佳答案
ActionbarTabsPager
正在扩展SherlockFragmentActivity
,而不是SherlockFragment
。更改
public class ActionbarTabsPager extends SherlockFragmentActivity {
至
public class ActionbarTabsPager extends SherlockFragment {
和
SherlockFragmentActivity mFragment = null;
至
Fragment mFragment = null;