Search code examples
androidandroid-fragmentsandroid-listfragment

Calling getListView().setChoiceMode() in ListFragment results in IllegalStateException


I am an Android programming newbie trying to move a working app from Activities to Fragments.

In the ScanningFragment (which should show a list of nearby Bluetooth devices) I have put the following code:

public class ScanningFragment extends ListFragment {
    private ScanningListener mListener;

    static class Beacon {
        public String address;
        public int rssi;
    }

    static class ViewHolder {
        public ImageView image1;
        public CheckedTextView text1;
    }

    private ArrayList<Beacon> mBeacons = new ArrayList<Beacon>();
    private ArrayAdapter<Beacon> mAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, 
            ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.activity_scanning, container, false);

        mAdapter = new ArrayAdapter<Beacon>(getActivity(),
                R.layout.rowlayout, 
                R.id.text1, 
                mBeacons) {

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder;

                if (convertView == null) {
                    LayoutInflater inflater = getActivity().getLayoutInflater();
                    convertView = inflater.inflate(R.layout.rowlayout, null);
                    holder = new ViewHolder();
                    holder.image1 = (ImageView) convertView.findViewById(R.id.image1);
                    holder.text1 = (CheckedTextView) convertView.findViewById(R.id.text1);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }

                Beacon beacon = (Beacon) getItem(position);
                holder.text1.setText(beacon.address);
                // TODO pass selected address here
                holder.text1.setChecked(beacon.address.equalsIgnoreCase(CommonConstants.ADDRESS_DEFAULT));
                int res = CommonConstants.rssi2res(beacon.rssi);
                holder.image1.setImageResource(res);                
                return convertView;
            }
        };     

        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); // Exception!
        setListAdapter(mAdapter);

        return view;
    }

Unfortunately the line

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

bombs out with an IllegalStateException (here fullscreen):

screenshot

And here is the trace (here fullscreen, sorry I don't know how to copy text from Eclipse Debug window):

trace

Does anybody please know, what am I doing wrong here?


Solution

  • Move that line to onViewCreated() instead of onCreateView(). In onCreateView() your ListView has not yet been created, so it will throw an IllegalStateException when you try to retrieve it.

    Also, ensure that activity_scanning.xml contains a ListView with android:id="@android:id/list".