简体   繁体   中英

ListView 'android.R.id.list' is not found in ListFragment

i am getting this errors.

Process: com.example.user.timey, PID: 17619
java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

I have already defined android:id="@android:id/list" in my listview in xml. This is my xml code:

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

And this is my java code:

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

Anyone knows whats wrong with my code? Thanks alot.

In your layout add id for ListView like this -

android:id="@+id/list"

Do not assign id to your views in layouts like @android:id/list .

Then in your onCreateView() method code get a reference to it -

ListView lv = (ListView) rootView.findViewById(R.id.list);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM