If I'm reading your question correctly, you're trying to access the Fragment
's View
(and children) using the Activity
's findViewById()
method. That shouldn't work since the Fragment
inflates its own layout and Fragments
are not traditional Views
.
If you know the Fragment
is instantiated and you can retrieve it, you can get an instance of the ViewGroup
using
yourFragment#getView().findViewById()
If not, you can make an interface that your Activity
implements with a method that accepts a ViewGroup
as the argument. Then in the Fragment's onCreateView()
, have the Fragment pass the ViewGroup
off to the interface. You can cast directly to your Activity
, but an interface is cleaner.
Eg
public class Fragment {
public interface ViewGroupCreateListener{
public void onViewGroupCreated (ViewGroup v);
}
private ViewGroupCreateListener listener;
public void onAttach (Activity a){
super.onAttach (a);
listener = (ViewGroupCreateListener) a;
}
public View onCreateView (/*all its arguments here*/){
View v = inflater.inflate (R.layout.your_layout);
ViewGroup group = v.findViewById (R.id.container);
listener.onViewGroupCreated(group);
return v;
}
}
Your Activity
would look something like:
public class MainActivity extends Activity implements ViewGroupCreateListener, OtherInterface1, OtherInterface2{
private ViewGroup mViewGroup;
public void onViewGroupCreated (ViewGroup v){
mViewGroup = v;
}
}
This is nice since if the pager re-instantiates the Fragment
, the Activity still gets a valid instance of the ViewGroup
.
Or, if depending on what you're actually trying to achieve with this ViewGroup
, you may be able to do this processing inside the Fragment
itself.