在ListFragment中找不到ListView'android.R.id.list'

时间:2016-03-06 18:25:28

标签: java android xml listview

我收到了这个错误。

  

处理:com.example.user.timey,PID:17619
                                                            java.lang.RuntimeException:您的内容必须具有其id为的ListView   属性是'android.R.id.list'

我已经在xml的listview中定义了android:id =“@ android:id / list”。 这是我的xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.timey.OneFragment">

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:divider="#E6E6E6"
    android:dividerHeight="1dp" >
</ListView>

</RelativeLayout>

这是我的java代码:

public class OneFragment extends ListFragment {

View rootView;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    DBController controller = new DBController(getActivity());
    ArrayList<HashMap<String, String>> scheduleList = controller.getAllSchedules();
    if (scheduleList.size()!=0){
        ListView lv = getListView();
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
        SimpleAdapter adapter = new SimpleAdapter(getActivity(), scheduleList, R.layout.view_schedule_entry, new String[] { "scheduleId",
                "scheduleName", "scheduleStartTime" }, new int[] {
                R.id.scheduleId, R.id.scheduleName, R.id.scheduleTime });
        setListAdapter(adapter);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    rootView = inflater.inflate(R.layout.fragment_one, container, false);
    return rootView;
}

任何人都知道我的代码有什么问题吗?非常感谢。

1 个答案:

答案 0 :(得分:1)

在你的布局中为ListView添加id,如下所示 -

android:id="@+id/list"

请勿在{{1​​}}等布局中为您的视图指定ID。

然后在您的@android:id/list方法代码中获取对它的引用 -

onCreateView()