在 ListFragment 中调用 getListView().setChoiceMode() 导致 IllegalStateException
Calling getListView().setChoiceMode() in ListFragment results in IllegalStateException
我是一名 Android 编程新手,试图将工作中的应用程序从 Activity 移动到 Fragments。
在 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 调试 window 复制文本):
有人知道吗,我在这里做错了什么?
将该行移至 onViewCreated()
而不是 onCreateView()
。在 onCreateView()
中,您的 ListView
尚未创建,因此当您尝试检索它时它会抛出一个 IllegalStateException
。
此外,确保 activity_scanning.xml
包含 ListView
和 android:id="@android:id/list"
。
我是一名 Android 编程新手,试图将工作中的应用程序从 Activity 移动到 Fragments。
在 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 调试 window 复制文本):
有人知道吗,我在这里做错了什么?
将该行移至 onViewCreated()
而不是 onCreateView()
。在 onCreateView()
中,您的 ListView
尚未创建,因此当您尝试检索它时它会抛出一个 IllegalStateException
。
此外,确保 activity_scanning.xml
包含 ListView
和 android:id="@android:id/list"
。