【问题标题】:Calling getListView().setChoiceMode() in ListFragment results in IllegalStateException在 ListFragment 中调用 getListView().setChoiceMode() 会导致 IllegalStateException
【发布时间】:2015-01-21 16:22:30
【问题描述】:

我是一名 Android 编程新手,试图将一个正在运行的应用从活动转移到片段。

在 ScanningFragment(应该显示附近蓝牙设备的列表)中,我输入了以下代码:

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;
    }

不幸的是这条线

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

以 IllegalStateException 轰炸(此处为 fullscreen):

这是跟踪(这里是fullscreen,抱歉我不知道如何从 Eclipse 调试窗口复制文本):

有人知道吗,我在这里做错了什么?

【问题讨论】:

    标签: android android-fragments android-listfragment


    【解决方案1】:

    将该行移至onViewCreated() 而不是onCreateView()。在onCreateView() 中,您的ListView 尚未创建,因此当您尝试检索它时它会抛出IllegalStateException

    另外,确保activity_scanning.xml 包含ListViewandroid:id="@android:id/list"

    【讨论】:

    • 感谢 +1,这很有道理。我应该只移动 1 行还是“inflate”调用之后的所有内容?
    • 可能只需要移动一个,但通常我在 onViewCreated 中进行所有视图初始化,并在 onCreateView 中进行膨胀。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 2016-01-20
    • 2018-01-03
    • 1970-01-01
    • 2015-09-30
    相关资源
    最近更新 更多