FragmentManager.beginTransaction()不能应用于(int,android.app.fragment)

时间:2014-12-31 17:58:28

标签: java android android-fragments navigation-drawer

我正在尝试在Android中创建导航抽屉,但我遇到了一些问题。我的MainActivity.java中有一个方法可以处理我在导航抽屉上的点击,并指导用户进行适当的活动。方法如下所示:

      @Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    android.app.Fragment objFragment = null;
    switch (position){
        case 0:
            objFragment= new menu1_Fragment();
            break;
        case 1:
            objFragment = new menu2_Fragment();
            break;
        case 2:
            objFragment = new menu3_Fragment();
            break;
    }
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container, objFragment)
            .commit();
}

我的问题是我在.replace(R.id.container, objFragment)的第三行到最后一行收到错误:FragmentTransaction cannot be applied to (int, android.app.fragment)

1 个答案:

答案 0 :(得分:5)

如果您使用SupportFragmentManager

,则需要使用支持片段

http://developer.android.com/reference/android/support/v4/app/Fragment.html http://developer.android.com/reference/android/support/v4/app/FragmentManager.html

VS

http://developer.android.com/reference/android/app/FragmentManager.html http://developer.android.com/reference/android/app/Fragment.html

你永远不能在两者之间混合搭配

// update the main content by replacing fragments
    android.support.v4.app.Fragment objFragment = null;
    switch (position){
        case 0:

清晰度,替换:

android.app.Fragment objFragment = null;

android.support.v4.app.Fragment objFragment = null;