-1

I'm trying to add a ListFragment to an Activity in Gingerbread, but Android crashes when the Activity is created and there is no stack trace. I have verified that I'm using the compatibility library imports. Here is my code for the ListFragment:

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

public class ListViewFragment extends ListFragment {
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    List listData = getListData().

    setListAdapter(new ListViewAdapter(getActivity(), listData));
  }

  class ListViewAdapter extends BaseAdapter implements ListAdapter {
    .
    .
    .
  }    
}

I also tried:

ListView listView = getListView();
listView.setAdapter(new ListViewAdapter(getActivity(), listData);

in place of the setListAdapter() call with the same results.

When I extend from just a Fragment and inflate a layout file with a ListView, it works fine. There seems to be something different when I try to use the inherent ListView in the ListFragment.

Any insight would be much appreciated.

NLam
  • 451
  • 1
  • 9
  • 22

2 Answers2

0

try it in onCreateView better than onActivityCreated.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = super.onCreateView( inflater, container, savedInstanceState );
    List listData = getListData().
    setListAdapter(new ListViewAdapter(getActivity(), listData));  
    return v;
}//met
Snicolas
  • 36,854
  • 14
  • 106
  • 172
0
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.your_layout, container, false);
    return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
        // Set up your list in here
}
Barak
  • 16,117
  • 9
  • 49
  • 84
  • As I mentioned above, I put a ListView in a layout and grabbed the listview handle with its id and it all worked fine. I'm more curious than anything why the getListView() doesn't work. And why there is no stack trace to give me more information. – NLam May 13 '12 at 03:16