Android调用导航抽屉中的新碎片活动
我试图调用导航抽屉布局中的碎片活动 但是当我被利用的时候Android调用导航抽屉中的新碎片活动,android,android-fragments,Android,Android Fragments,我试图调用导航抽屉布局中的碎片活动 但是当我被利用的时候 FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); 然后发生错误 FragmentTransaction类型中的方法replaceint,Fragment为 不适用于参数int,Fragment 请帮我解决这
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
然后发生错误
FragmentTransaction类型中的方法replaceint,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;
如果是android.support.v4.app.FragmentActivity,则应使用返回android.support.v4.app.FragmentManager的getSupportFragmentManager,而不是返回android.app.FragmentManager的getFragmentManager
替代进口
import android.app.Fragment;
import android.app.FragmentManager;
与
然后
final FragmentManager fragmentManager = getSupportFragmentManager();
假设您的片段扩展了android.support.v4.app.Fragment
更新:
要在活动之外使用FragmentManager,请在创建对象时传递它。比如说
public final class NavDrawer {
private final FragmentManager mFragmentManager;
public NavDrawer(final FragmentManager fragmentManager) {
mFragmentManager = fragmentManager;
}
....
如果从活动创建NavDrawer
new NavDrawer(getSupportFragmentManager())
你能发布片段的导入吗??。你在使用支持库中的片段吗?我已经发布了我的导入和片段class@Raghunandan请查看我的更新问题,并建议我如何修复它您需要使用getSupportFragmentManager我可以使用“getSupportFragmentManager”吗感谢您的重播,但出现错误getSupportFragmentManager方法未定义类型NavDrawer@Kuldeep然后将碎片管理器传递给NavDrawer构造函数。我不知道如何传递请建议我如何传递,我更新了我的所有导入和碎片类see@Kuldeep更新了答案。如果NavDrawer是您的类,则通过构造函数将其传递给NavDrawer。
new NavDrawer(getSupportFragmentManager())