无法在导航栏中调用片段

时间:2014-02-23 16:33:40

标签: android android-fragments navigation-drawer slidingmenu

我已按照脚本从here制作导航抽屉,但我收到错误。

 private class SlideMenuClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // display view for selected nav drawer item
        displayView(position);
    }
}

/**
 * Diplaying fragment view for selected nav drawer list item
 * */
private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new HomeFragment();
        break;
    case 1:
        //aaa
        break;

    default:
        break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
        .replace(R.id.frame_container, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

错误在此代码中:

fragment = new HomeFragment();

它说“类型不匹配:无法从HomeFragment转换为Fragment”

仅供参考,我使用 import android.app.Fragment; import android.app.FragmentManager;

但当我将其转换为

import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager;

错误在这一行

`FragmentManager fragmentManager = getSupportFragmentManager();`

当我将活动更改为FragmentActivity时,没有错误,但我无法打开我的滑动菜单......

谁能帮帮我?对不起我的英文..:D

0 个答案:

没有答案