Create listview in fragment android

后端 未结 5 552
难免孤独
难免孤独 2020-12-05 07:24

As the title I want to create a listview with custom row in Fragment. My code below.

Fragment class

public class PhotosFragment extends Fragment{

pu         


        
5条回答
  •  南方客
    南方客 (楼主)
    2020-12-05 07:54

    I guess your app crashes because of NullPointerException.

    Change this

    ListView lv = (ListView)getActivity().findViewById(R.id.lv_contact);
    

    to

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

    assuming listview belongs to the fragment layout.

    The rest of the code looks alright

    Edit:

    Well since you said it is not working i tried it myself

    enter image description here

提交回复
热议问题