Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Fragment no view found for ID?

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?

like image 430
coder_For_Life22 Avatar asked Sep 21 '11 23:09

coder_For_Life22


People also ask

What is FragmentContainerView?

FragmentContainerView is a customized Layout designed specifically for Fragments. It extends FrameLayout , so it can reliably handle Fragment Transactions, and it also has additional features to coordinate with fragment behavior.

What is Android R id content?

android.R.id.content gives you the root element of a view, without having to know its actual name/type/ID.

What is onCreateView in Android?

onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment. onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.

How do I attach a fragment to an activity?

Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.


2 Answers

I was having this problem too, until I realized that I had specified the wrong layout in setContentView() of the onCreate() method of the FragmentActivity.

The id passed into FragmentTransaction.add(), in your case R.id.feedContentContainer, must be a child of the layout specified in setContentView().

You didn't show us your onCreate() method, so perhaps this is the same problem.

like image 74
howettl Avatar answered Oct 19 '22 15:10

howettl


This error also occurs when having nested Fragments and adding them with getSupportFragmentManager() instead of getChildFragmentManager().

like image 44
Malachiasz Avatar answered Oct 19 '22 17:10

Malachiasz