Android使用ShelockFragment而不是Fragment

时间:2015-04-02 13:59:47

标签: android android-fragments actionbarsherlock

我使用以下代码在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();

1 个答案:

答案 0 :(得分:1)

ActionbarTabsPager正在延长SherlockFragmentActivity,而不是SherlockFragment。变化

public class ActionbarTabsPager extends SherlockFragmentActivity {

public class ActionbarTabsPager extends SherlockFragment {

SherlockFragmentActivity mFragment = null;

Fragment mFragment = null;