Question

I have a Fragment that is part of a ViewPager. In this Fragment I have a ViewGroup child. Now, why in my MainActivity's onCreate() after having instantiated my ViewPager and adapter, my Container is getting null?

Here is my onCreate():

private MyAdapter mAdapter;
private ViewPager mPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mAdapter = new MyAdapter(getSupportFragmentManager());

    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);

    mContainerView = (ViewGroup) findViewById(R.id.container);
    //Here mContainerView is already null
    ...
}

Here is the Fragment that is part of a ViewPager that contains mContainerView

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<ScrollView android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- This is my ViewGroup -->
   <LinearLayout android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:showDividers="middle"
        android:divider="?android:dividerHorizontal"
        android:animateLayoutChanges="true"
        android:paddingLeft="16dp"
        android:paddingRight="16dp" />

</ScrollView>

<TextView android:id="@android:id/empty"
    style="?android:textAppearanceSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:padding="32dp"
    android:text="@string/message_empty_layout_changes"
    android:textColor="?android:textColorSecondary" />

</FrameLayout>
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top