-2

I get a null pointer exception error when i try to give a name to it. I mean it gives an error while doing R.id.contacts which probably means it's layout doesn't have the listview.

     @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    DataBaseOperations hell = new DataBaseOperations(getActivity());
    SQLiteDatabase db = hell.getReadableDatabase();
    String[] columns = {mDatabase.Tableinfo.ID,mDatabase.Tableinfo.Contacts_phone,mDatabase.Tableinfo.Contacts_name,mDatabase.Tableinfo.Contact_pic};
    Cursor Contacts = db.query(mDatabase.Tableinfo.contacts, columns, null, null, null, null, null);
    ContactsAdapter adapter = new ContactsAdapter(getContext(), Contacts,0);
    ListView iv = (ListView)lview.findViewById(R.id.contacts);
    iv.setAdapter(adapter);
    Contacts.close();
    lview = inflater.inflate(R.layout.fragment_contacts_list, container, false);
    return lview;
}

as it inflates the layout named fragment-contacts_list.Here is the code for that

   <?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/contacts"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_weight="1"
tools:listitem="@layout/fragment_contacts" />

Why is my listview null???

Umer Asif
  • 441
  • 1
  • 4
  • 15

1 Answers1

1

Try this, you have to inflate layout before "search" object:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    DataBaseOperations hell = new DataBaseOperations(getActivity());
    SQLiteDatabase db = hell.getReadableDatabase();
    String[] columns = {mDatabase.Tableinfo.ID,mDatabase.Tableinfo.Contacts_phone,mDatabase.Tableinfo.Contacts_name,mDatabase.Tableinfo.Contact_pic};
    Cursor Contacts = db.query(mDatabase.Tableinfo.contacts, columns, null, null, null, null, null);
    ContactsAdapter adapter = new ContactsAdapter(getContext(), Contacts,0);
    Contacts.close();
    lview = inflater.inflate(R.layout.fragment_contacts_list, container, false);
    ListView iv = (ListView)lview.findViewById(R.id.contacts);
    iv.setAdapter(adapter);
    return lview;
}
Michele Lacorte
  • 5,323
  • 7
  • 32
  • 54