Android在导航抽屉中调用新的碎片活动

时间:2014-04-09 16:42:16

标签: android android-fragments

我试图在导航抽屉布局中调用碎片化活动

但是当我使用时

 FragmentManager fragmentManager = getFragmentManager();
  fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

然后发生错误

  

FragmentTransaction类型中的方法replace(int,Fragment)是   不适用于参数(int,Fragment)

请帮助我如何解决这个问题

我的零碎课程

package com.newmaynard;



  import com.newmaynard.listadapter.AllList;

   import android.os.Bundle;
   import android.support.v4.app.Fragment;
   import android.view.LayoutInflater;
  import android.view.View;
   import android.view.ViewGroup;
   import android.widget.AbsListView;
  import android.widget.ListView;

  public class AllListItem extends Fragment {

ListView listTalent;
ListView listPresent;

String[] strTalent;
String[] strPrasent;

public AllListItem()
{

}

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
         Bundle savedInstanceState) {
     View rootView = inflater.inflate(R.layout.alllist, container, false);


    strPrasent= new String[] {"All","Preaparation","Purpose","Personality","Passion","Presence"};
    strTalent= new String[]{"All","Valued","Involved","Development","Inspired"};



    listPresent= (ListView) rootView.findViewById(R.id.listpresent);
    listTalent= (ListView) rootView.findViewById(R.id.listtalant);

    listPresent.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
    listTalent.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);


    AllList adapterTalent= new AllList(getActivity(), strTalent);
    listTalent.setAdapter(adapterTalent);

    AllList adapterPresent= new AllList(getActivity(), strPrasent);
    listPresent.setAdapter(adapterPresent);

  return rootView;

}


}

我的导入是

import com.newmaynard.listadapter.*;
import android.annotation.SuppressLint;
 import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
 import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

1 个答案:

答案 0 :(得分:0)

如果它是android.support.v4.app.FragmentActivity,那么您应该使用getSupportFragmentManager()(返回android.support.v4.app.FragmentManager)而不是getFragmentManager()(返回android.app.FragmentManager)。< / p>

替换进口

import android.app.Fragment;
import android.app.FragmentManager;

使用

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

然后

final FragmentManager fragmentManager = getSupportFragmentManager();

假设您的碎片延伸android.support.v4.app.Fragment

更新: 要在Activity之外使用FragmentManager,请在创建对象时传递它。例如

public final class NavDrawer {

    private final FragmentManager mFragmentManager;

    public NavDrawer(final FragmentManager fragmentManager) {
        mFragmentManager = fragmentManager;
    }

....

如果从Activity

创建NavDrawer
new NavDrawer(getSupportFragmentManager())