DaveSav
DaveSav

Reputation: 1374

findFragmentById is always null

Trying to find a ListFragment in the xml layout is always returning null. I'm using SherlockFragmentActivity with a SherlockListFragment. Here is the code:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_custom_layout);

        adapter = new My_Custom_Adapter(this, My_Object, My_Object_2);

    }

 // Create the list fragment and add it as the list content.
    if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
        mListFragment list = new mListFragment();
        getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit();

    }
}

public static class mListFragment extends SherlockListFragment {

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setListAdapter(adapter);
        setListShown(true);
    }

}

This is a shortened version of the xml layout:

<RelativeLayout
   android:id="@+id/top_control_bar">
    <!-- Text Views -->
</RelativeLayout>
<RelativeLayout >
    <!-- Button -->
</RelativeLayout>
<LinearLayout
    android:id="@+id/start_data"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/top_control_bar"
    android:orientation="vertical" >

    <fragment 
    android:name="android.support.v4.app.ListFragment"
    android:id="@android:id/content"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>

findFragmentById(android.R.id.content)

always returns null. Why can't it find the fragment within my layout?

Upvotes: 0

Views: 1186

Answers (3)

Asghar Nazir
Asghar Nazir

Reputation: 369

Try this in mListFragment class override onCreate() and call setRetainInstance(true);

public static class mListFragment extends SherlockListFragment {

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setListAdapter(adapter);
    setListShown(true);
}

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
  }

}

Upvotes: 0

viktor0710
viktor0710

Reputation: 131

The problem that you have is that you declared in the xml the Fragment as a ListFragment.

getSupportFragmentManager().findFragmentById(android.R.id.content) uses Support fragment manager, so you will need a SupportMapFragment

Try this:

<fragment 
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:id="@android:id/content"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
/>

Upvotes: 1

DigCamara
DigCamara

Reputation: 5568

Shouldn't

android:id="@android:id/content"

be

android:id="@+id/content"

?

From the documentation it seems like you are using the wrong syntax.

I'm also not sure your chained methods will work correctly as written. Change

 getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit();

to

FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(android.R.id.content, list);
transaction.commit();

even then, I honestly don't know what you'd be trying to do there. You are aware that FragmentTransaction's add method as you're using it is trying to add a fragment to a container which, in your if, already was established as having a null value?

Upvotes: 0

Related Questions