如何一起使用Nav Drawer和FragmentActivity

时间:2015-04-21 22:50:45

标签: android android-fragments navigation-drawer

应用程序不能正确构建,因为我认为它在构建导航抽屉时正在寻找片段cruisespeed_Fragment,实际上是fragmentactivitycruisespeed_Fragment是片段活动的原因是它是一个标签式布局,我使用this教程将它放在一起。

MainActivity.java

@Override
public void onNavigationDrawerItemSelected(int position) {

    Fragment objFragment = null;

    switch (position) {
        case 0:
            objFragment = new project_Fragment();
            break;
        case 1:
            objFragment = new cruisespeed_Fragment();
                break;
        case 2:
            objFragment = new satflow_Fragment();
            break;
        case 3:
            objFragment = new network_Fragment();
            break;
        }
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container, objFragment)
            .commit();
}

And in cruisespeed_Fragment.java

public class cruisespeed_Fragment extends FragmentActivity implements ActionBar.TabListener {

private ViewPager viewPager;
private CSPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = {"Top Rated", "Games", "Movies"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new CSPagerAdapter(getFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }
}

我得到的错误是Error:(62, 31) error: incompatible types required: Fragment found: cruisespeed_Fragment

出于某种原因,logcat目前还没有输出任何内容,但一旦我再次排序,我就会更新。

2 个答案:

答案 0 :(得分:0)

FragmentActivity扩展Activity而非Fragment您需要让_Fragment()类从某种形式的Fragment扩展

答案 1 :(得分:0)

如果不亲自测试您的代码,我很难说清楚,但我的理解是FragmentActivityActivity用来托管android.support.v4.app.Fragment,而不是android.app.Fragment {1}}

因此,您无法使用以下代码启动FragmentActivity:

FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
        .replace(R.id.container, objFragment)
        .commit();

您需要使用Intent。此外,请确保您不会意外地将支持Fragments与常规Fragments混合使用。 This帖子详细介绍了差异。