与getSupportFragmentManager时(findFragmentByTag()的返回null) [英] findFragmentByTag() is returning null when working with getSupportFragmentManager()
问题描述
我的支持库动作条的工作,因为我使用的是旧的最低SDK。在活动中,我使用FragmentTabHost因为我有3个标签。该动作条也有一个搜索查看,所以当进行检索,第三个选项卡切换出与搜索的结果。
我能够从搜索查看intput得到,但我无法转出的第三个标签时,我有一个搜索结果。我用这作为一个例子:
<一href=\"http://stackoverflow.com/questions/18120510/dynamically-changing-the-fragments-inside-a-fragment-tab-host\">Dynamically改变片段片段选项卡里面的主机?
我的问题是,当我尝试去的第三个标签的参考,我用getSupportFragmentManager()。findFragmentByTag()返回的片段总是空。
我的基本容器,有助于在标签交换多个片段:
公共类BaseContainerFragment扩展片段{ 公共无效replaceFragment(片段片段,布尔addToBackStack){
FragmentTransaction交易= getChildFragmentManager()调用BeginTransaction()。
如果(addToBackStack){
transaction.addToBackStack(NULL);
}
transaction.replace(R.id.container_framelayout,片段);
器transaction.commit();
getChildFragmentManager()executePendingTransactions()。
} 公共布尔popFragment(){
//Log.e(\"test,流行音乐的片段:+ getChildFragmentManager()getBackStackEntryCount());
布尔isPop = FALSE;
如果(getChildFragmentManager()getBackStackEntryCount()方式&gt; 0){
isPop = TRUE;
getChildFragmentManager()popBackStack()。
}
返回isPop;
}
}
扩展BaseContainerFragment的容器
公共类LibraryContainerFragment扩展BaseContainerFragment { 私人布尔mIsViewInited; @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
Log.e(测试,标签1 oncreateview);
返回inflater.inflate(R.layout.container_fragment,NULL);
} @覆盖
公共无效onActivityCreated(捆绑savedInstanceState){
super.onActivityCreated(savedInstanceState);
Log.e(测试,上的活动选项卡1容器创建);
如果(!mIsViewInited){
mIsViewInited = TRUE;
initView();
// setRetainInstance(真);
}
} 私人无效initView(){
Log.e(测试,标签1初始化视图); replaceFragment(新LibraryFragment,FALSE); }
}
用于转出片段(container_fragment.xml)的XML:
&LT;?XML版本=1.0编码=UTF-8&GT?;
&LT;的FrameLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / container_framelayout
机器人:layout_width =match_parent
机器人:layout_height =match_parent&GT;&LT; /&的FrameLayout GT;
我的主要活动:
公共类BookSetup扩展ActionBarActivity { //为了访问SlidingMenu库
私人SlidingMenu slidingMainMenu;
私人FragmentTabHost mTabHost;
私人SlidingMenu slidingContextMenuFavourites;
私人SlidingMenu slidingContextMenuMyPrayerBook;
私人SlidingMenu slidingContextMenuLibrary;
私人android.support.v4.app.FragmentManager fragmentManager; LibraryContainerFragment libraryContainerFragment;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
//设置操作栏中可点击左上角的按钮
android.support.v7.app.ActionBar栏= getSupportActionBar(); bar.setDisplayHomeAsUpEnabled(假);
bar.setHomeButtonEnabled(真);
bar.setIcon(R.drawable.main_menu); //设置选项卡式浏览
bar.setDisplayShowTitleEnabled(假);
//设置标签
fragmentManager = getSupportFragmentManager();
mTabHost =(FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(这一点,fragmentManager,R.id.realtabcontent); //添加标签
mTabHost.addTab(mTabHost.newTabSpec(最爱)。setIndicator(的getString(R.string.favourites))
FavouritesFragment.class,NULL); mTabHost.addTab(mTabHost.newTabSpec(我的书)。setIndicator(的getString(R.string.my_book))
MyBookFragment.class,NULL);
mTabHost.addTab(mTabHost.newTabSpec(库)。setIndicator(的getString(R.string.library))
LibraryContainerFragment.class,NULL); mTabHost.setCurrentTab(2);
// mTabHost。 //创建时,滑动动画活动启动
overridePendingTransition(R.anim.slide_in_from_right,R.anim.slide_out_to_left);
} @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){ //充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.main_menu_with_search_context_menu,菜单);
SearchManager的SearchManager的=(SearchManager的)getSystemService(Context.SEARCH_SERVICE);
//搜索查看搜索查看=(搜索查看)menu.findItem(R.id.action_search).getActionView(); 最终的菜单项searchMenuItem = menu.findItem(R.id.action_search);
搜索查看=(android.support.v7.widget.SearchView)MenuItemCompat.getActionView(searchMenuItem);
//搜索查看=(搜索查看)menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(SearchManager的
.getSearchableInfo(getComponentName()));
//searchView.requestFocus();
searchView.requestFocusFromTouch(); //searchView.setIconifiedByDefault(true);
//监听器操作栏中找到搜索输入,当放大镜
//点击
searchView.setOnQueryTextListener(新SearchView.OnQueryTextListener(){ //当一个搜索字符串提交激活
@覆盖
公共布尔onQueryTextSubmit(查询字符串){
// TODO:查询是从搜索视图中的文本,你点击搜索后, 如果(查询!= NULL){
//如果找到结果,然后切换片段
如果(!sectionsFound.isEmpty()){ //初始化搜索片段和发送数据的包吧
SearchLibraryResultFragment片段=新SearchLibraryResultFragment();
束束=新包();
bundle.putParcelableArrayList(价值观,
(ArrayList的&LT ;?延伸Parcelable&GT;)sectionsFound);
bundle.putStringArrayList(名,sectionNames);
fragment.setArguments(包); mTabHost.setCurrentTab(2);
libraryContainerFragment =(LibraryContainerFragment)fragmentManager.findFragmentByTag(库);
((BaseContainerFragment)libraryContainerFragment.getParentFragment()).replaceFragment(片段,真实);
返回true;
}
}); }
}
这是始终处于BookSetup.java返回null行:
((BaseContainerFragment)libraryContainerFragment.getParentFragment()).replaceFragment(片段,真实);
正在使用 ChildFragmentManager
来取代你的片段
吧?
这可能是你的问题,而不是
libraryContainerFragment =(LibraryContainerFragment)fragmentManager.findFragmentByTag(库);
使用
libraryContainerFragment =(LibraryContainerFragment)getChildFragmentManager()findFragmentByTag(库)。
I am working with the support library ActionBar because I'm using an older minimum SDK. In the activity, I am using FragmentTabHost because I have 3 tabs. The ActionBar also has a SearchView, so when a search is made, the 3rd tab is switched out with the results of the search.
I am able to get intput from the SearchView, but I am unable to switch out the 3rd tab when I have a search result. I am using this as an example: Dynamically changing the fragments inside a fragment tab host?
My problem is that when I try to get a reference to the 3rd tab, and I use getSupportFragmentManager().findFragmentByTag() , the fragment being returned is always null.
My base container that helps in swapping multiple fragments in a tab:
public class BaseContainerFragment extends Fragment{
public void replaceFragment(Fragment fragment, boolean addToBackStack) {
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.replace(R.id.container_framelayout, fragment);
transaction.commit();
getChildFragmentManager().executePendingTransactions();
}
public boolean popFragment() {
//Log.e("test", "pop fragment: " + getChildFragmentManager().getBackStackEntryCount());
boolean isPop = false;
if (getChildFragmentManager().getBackStackEntryCount() > 0) {
isPop = true;
getChildFragmentManager().popBackStack();
}
return isPop;
}
}
A container that extends BaseContainerFragment
public class LibraryContainerFragment extends BaseContainerFragment {
private boolean mIsViewInited;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.e("test", "tab 1 oncreateview");
return inflater.inflate(R.layout.container_fragment, null);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e("test", "tab 1 container on activity created");
if (!mIsViewInited) {
mIsViewInited = true;
initView();
// setRetainInstance(true);
}
}
private void initView() {
Log.e("test", "tab 1 init view");
replaceFragment(new LibraryFragment, false);
}
}
The xml used to switch out fragments (container_fragment.xml):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container_framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
My main activity:
public class BookSetup extends ActionBarActivity {
// For accessing SlidingMenu library
private SlidingMenu slidingMainMenu;
private FragmentTabHost mTabHost;
private SlidingMenu slidingContextMenuFavourites;
private SlidingMenu slidingContextMenuMyPrayerBook;
private SlidingMenu slidingContextMenuLibrary;
private android.support.v4.app.FragmentManager fragmentManager;
LibraryContainerFragment libraryContainerFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Settings for the clickable top-left button in the action bar
android.support.v7.app.ActionBar bar = getSupportActionBar();
bar.setDisplayHomeAsUpEnabled(false);
bar.setHomeButtonEnabled(true);
bar.setIcon(R.drawable.main_menu);
// Setting up tabbed navigation
bar.setDisplayShowTitleEnabled(false);
// Setting up tabs
fragmentManager = getSupportFragmentManager();
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, fragmentManager, R.id.realtabcontent);
// Add tabs
mTabHost.addTab(mTabHost.newTabSpec("Favourites").setIndicator(getString(R.string.favourites) ),
FavouritesFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("My Book").setIndicator(getString(R.string.my_book) ),
MyBookFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("Library").setIndicator(getString(R.string.library) ),
LibraryContainerFragment.class, null);
mTabHost.setCurrentTab(2);
//mTabHost.
// Creates a sliding animation when activity is started
overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_to_left);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu_with_search_context_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
//SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
final MenuItem searchMenuItem = menu.findItem(R.id.action_search);
searchView = (android.support.v7.widget.SearchView) MenuItemCompat.getActionView(searchMenuItem);
//searchView = (SearchView)menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
//searchView.requestFocus();
searchView.requestFocusFromTouch();
//searchView.setIconifiedByDefault(true);
// Listener for the search input found in the action bar, when the magnifying glass is
// clicked
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
// Activated when a search string is submitted
@Override
public boolean onQueryTextSubmit(String query) {
// TODO : query is the text from the search view after you clicked search
if(query != null){
// If results are found, then switch the fragments
if(!sectionsFound.isEmpty()){
// Initialize the search fragment and send bundles of data to it
SearchLibraryResultFragment fragment = new SearchLibraryResultFragment();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("values",
(ArrayList<? extends Parcelable>) sectionsFound);
bundle.putStringArrayList("names", sectionNames);
fragment.setArguments(bundle);
mTabHost.setCurrentTab(2);
libraryContainerFragment = (LibraryContainerFragment)fragmentManager.findFragmentByTag("Library");
((BaseContainerFragment) libraryContainerFragment.getParentFragment() ).replaceFragment(fragment,true );
return true;
}
});
}
}
This is the line that always returns null in BookSetup.java:
((BaseContainerFragment) libraryContainerFragment.getParentFragment() ).replaceFragment(fragment,true );
You are using the ChildFragmentManager
to replace your Fragments
right?
That might be your problem, instead of
libraryContainerFragment = (LibraryContainerFragment)fragmentManager.findFragmentByTag("Library");
use
libraryContainerFragment = (LibraryContainerFragment) getChildFragmentManager().findFragmentByTag("Library");
这篇关于与getSupportFragmentManager时(findFragmentByTag()的返回null)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!