Search code examples
androidactionbarsherlockandroid-listfragment

SherlockListFragment Causing IllegalStateException Must Attach to SherlockListActivity


I have a class which creates a behindMenu for a Sliding Menu as follows:

public class BehindMenuFragment extends ListFragment {

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    String[] categories = getResources().getStringArray(R.array.categories);
    CategoryAdapter adapter = new CategoryAdapter(getActivity());
    int cats = categories.length;
    for (int i = 0; i < cats; i++) {
        adapter.add(new CategoryItem(categories[i], getIcon(categories[i])));
        }
    setListAdapter(adapter);
}

It gets called from another activity MainActivity with the following code:

getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.menu_frame, new BehindMenuFragment ())
    .commit();

The above code works fine no problems, I want to insert an Action Bar using ActionBarSherlock to the behind menu I've tried it in every way possible to no avail.

Extending BehindMenuFragment as a SherlockListFragment gives me the following error:

IllegalStateException: BehindMenuFragment must be attached to a SherlockFragmentActivity

Im at a loss and don't know how to move forward with this problem please help!


Solution

  • It appears you haven't done step four:

    Setup with ActionBarSherlock

    1. Setup as above.
    2. Checkout a clean copy of ActionBarSherlock and import into your Eclipse workspace.
    3. Add ActionBarSherlock as a dependency to SlidingMenu
    4. Go into the SlidingActivities that you plan on using make them extend Sherlock___Activity instead of ___Activity.

    Having never worked with this library I would guess that you should change MainActivity to:

    public class MainActivity extends SherlockSlidingFragmentActivity {
    

    Now you can use a SherlockListFragment:

    public class BehindMenuFragment extends SherlockListFragment {
    

    And add Sherlock's ActionBar.