mardi 16 juin 2015

Android Database null fragments

It keeps saying

     getReadableDatabse cant be invoked on null object which is the database. 

So my DataBase is null for some reason.

Main Activity: sent from fragment a to activity and activity updates fragment B

// Sends athlete information to athlete list
@Override
public void send(String confirmation) {
    if(confirmation == "Athlete Added!") {
        AthletesList athletesList = getAthleteListFragment();
        getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, athletesList, "athletesList");
        athletesList.addAthleteToList();
    }else{
        Toast.makeText(this, "Error: message confirmation incorrect!", Toast.LENGTH_SHORT).show();
    }
}
public AthletesList getAthleteListFragment(){
    AthletesList athletesList = (AthletesList) getSupportFragmentManager().findFragmentByTag("athletesList");
    if(athletesList == null){
        athletesList = new AthletesList();
    }
    return athletesList;
}

Create DataBase instance here in Activity created. This is a Fragment.

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    db = new dbHelper(getActivity());
}

Gets to the first log message but then says my DataBase is null.

public void addAthleteToList(){
    Log.e("", "Getting to addAthleteToList()");
    db.getReadableDatabase();
    Athlete athlete = db.getLastInsertedAthlete();
    db.close();
    Log.e("", "Athlete ID to be added: " + athlete.getAthleteID());

Aucun commentaire:

Enregistrer un commentaire