title: How to populate listview with database data inside a fragment?
body: <p>I started to use fragments since I got comfortable with using activitys and I just wanna take things to a another level. What I learned so far is passing data between fragments and now I wanna try to do something which includes databases.</p>
  
  <p>This is just an example app which will show me how to deal with this kind of stuff.</p>
  
  <p>So basically I have a fragment for adding contacts and a second fragment to show the first name of the contact in a listview. I'm adding the contacts in a database and that part works fine. But when I try to show the data from a database inside a fragment that contains a listview my app gets crashed.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity implements Contacts_Add.sendData {
  
  DatabaseHandler handler;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      handler = new DatabaseHandler(this, null, null, 1);
  
      //____________________________PRINT DATABASE_____________________
  
      FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
      fragmentTransaction.replace(R.id.fragment_container, new Contacts_All(), "allFrag");
      fragmentTransaction.commit();
  
      getSupportFragmentManager().executePendingTransactions();
  
      Contacts_All fragAll = (Contacts_All) getSupportFragmentManager().findFragmentByTag("allFrag");
      fragAll.printDatabase();
  
      //______________________________________________________________________
  
  //etValues method is outside of onCreate I just didnt put the whole code.
  
    @Override 
  public void etValues(String fname, String lname, String phone, String email) {
  
      ContactsObjects contact = new ContactsObjects(fname, lname, phone, email);
  
      handler.insertContact(contact);
  
      FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
      fragmentTransaction.replace(R.id.fragment_container, new Contacts_All(), "allFrag");
      fragmentTransaction.commit();
  
      getSupportFragmentManager().executePendingTransactions();
  
      Contacts_All fragAll = (Contacts_All) getSupportFragmentManager().findFragmentByTag("allFrag");
      fragAll.printDatabase();
  
  
  }
  </code></pre>
  
  <p>All contacts fragment:</p>
  
  <pre><code>public class Contacts_All extends ListFragment {
  
  
  DatabaseHandler handler;
  
  public Contacts_All() {
      // Required empty public constructor
  }
  
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
  
      View view = inflater.inflate(R.layout.contacts_all, container, false);
  
  
      return view;
  }
  
  public void printDatabase() {
  
      List&lt;ContactsObjects&gt; contactsList = handler.getAllContacts();
      ArrayList&lt;ContactsObjects&gt; newContactsList = new ArrayList&lt;ContactsObjects&gt;();
      for (ContactsObjects cn : contactsList) {
  
          newContactsList.add(cn);
  
      }
  
      ListAdapter la = new ArrayAdapter&lt;ContactsObjects&gt;(getActivity(), android.R.layout.simple_list_item_1, newContactsList);
  
      setListAdapter(la);
  
  
  }
  
  
  }
  </code></pre>
  
  <p>DatabaseHandler:</p>
  
  <pre><code> public List&lt;ContactsObjects&gt; getAllContacts() {
      List&lt;ContactsObjects&gt; contactList = new ArrayList&lt;ContactsObjects&gt;();
  
      String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;
  
      SQLiteDatabase db = this.getWritableDatabase();
      Cursor cursor = db.rawQuery(selectQuery, null);
  
  
      if (cursor.moveToFirst()) {
          do {
              ContactsObjects contact = new ContactsObjects();
              contact.set_id(Integer.parseInt(cursor.getString(cursor.getColumnIndex("_id"))));
              contact.setFirstName(cursor.getString(cursor.getColumnIndex("firstname")));
              contact.setLastName(cursor.getString(cursor.getColumnIndex("lastname")));
              contact.setPhoneNumber(cursor.getString(cursor.getColumnIndex("phonenumber")));
              contact.setEmail(cursor.getString(cursor.getColumnIndex("email")));
  
              contactList.add(contact);
          } while (cursor.moveToNext());
      }
  
  
      return contactList;
  }
  </code></pre>
  
  <p>logcat error:
  1st error is at this line:</p>
  
  <pre><code> List&lt;ContactsObjects&gt; contactsList = handler.getAllContacts();
  </code></pre>
  
  <p>2nd error is at this line:</p>
  
  <pre><code>  fragAll.printDatabase(); //in onCreate 
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/15Wio.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/15Wio.jpg" alt="logcat"></a></p>