I have a fragment I am trying to add into a view.
FragmentManager fragMgr=getSupportFragmentManager();
feed_parser_activity content = (feed_parser_activity)fragMgr
.findFragmentById(R.id.feedContentContainer);
FragmentTransaction xaction=fragMgr.beginTransaction();
if (content == null || content.isRemoving()) {
content=new feed_parser_activity(item.getLink().toString());
xaction
.add(R.id.feedContentContainer, content)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(null)
.commit();
Log.e("Abstract", "DONE");
}
When this code is executed I get the following error in debug..
java.lang.IllegalArgumentException: No view found for id 0x7f080011
for fragment feed_parser_activity{41882f50 #2 id=0x7f080011}
feed_parser_activity
is a Fragment that is set to Fragment layout in xml.
I am using a FragmentActivity to host the Fragment Layout holding the feed_parser_layout
.
Am I coding this correctly above?
I had this problem (when building my UI in code) and it was caused by my
ViewPager
(that showedFragment
s) not having an ID set, so I simply usedpager.setID(id)
and then it worked.This page helped me figure that out.
Inside your fragment use this:
Another scenario I have met. If you use nested fragments, say a ViewPager in a Fragment with it's pages also Fragments.
When you do Fragment transaction in the inner fragment(page of ViewPager), you will need
getActivity() is the key here. ...
My mistake was on the
FragamentTransaction
.I was doing this
t.replace(R.layout.mylayout);
instead oft.replace(R.id.mylayout);
The difference is that one is the layout and the other is a reference to the
layout(id)
With nested fragments
By using
getChildFragmentManager()
instead ofgetActivity.getSupportFragmentManager()
resolved crash java.lang.IllegalArgumentException: No view found for id for me.In my case I was trying to show a DialogFragment containing a pager and this exception was thrown when the FragmentPagerAdapter attempted to add the Fragments to the pager. Based on howettl answer I guess that it was due to the Pager parent was not the view set in setContentView() in my FragmentActivity.
The only change I did to solve the problem was to create the FragmentPagerAdapter passing in a FragmentMager obtained by calling getChildFragmentManager(), not the one obtained by calling getFragmentManager() as I normally do.
}