Android数据库空片段

时间:2015-06-17 03:41:27

标签: android sqlite android-fragments android-sqlite

一直在说

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

因此,我的DataBase由于某种原因而为空。

DBHelper:

// Create tables or drop if new version
public dbHelper(Context context){
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public Athlete getLastInsertedAthlete(){

    Log.e("", "TEST!!!!!!!!");

    Athlete athlete = new Athlete();
    SQLiteDatabase db = this.getReadableDatabase();


    Cursor cursor = db.rawQuery("SELECT MAX(" + COL_ATHLETE_ID + ") AS " + COL_ATHLETE_ID + ", " +
            COL_FIRST_NAME + ", " + COL_LAST_NAME + ", " + COL_AGE + ", " + COL_GRADE +
            " FROM " + TABLE_ATHLETES, null);
    cursor.moveToFirst();
    int lastAthleteID = cursor.getInt(cursor.getColumnIndex(COL_ATHLETE_ID));
    Log.e("", "Athlete ID: " + lastAthleteID);
    athlete.setAthleteID(lastAthleteID);

    cursor = db.rawQuery("SELECT " + COL_EVENT + " FROM " + TABLE_ATHLETES +
    " INNER JOIN " + TABLE_ATHLETES_EVENTS + " ON " + TABLE_ATHLETES + "." + COL_ATHLETE_ID +
            " = " + TABLE_ATHLETES_EVENTS + "." + COL_ATHLETE_ID +
            " INNER JOIN " + TABLE_EVENTS + " ON " + TABLE_ATHLETES_EVENTS + "." + COL_EVENT_ID +
            " = " + TABLE_EVENTS + "." + COL_EVENT_ID +
            " WHERE " + TABLE_ATHLETES + "." + COL_ATHLETE_ID + " = ?", new String[] {String.valueOf(athlete.getAthleteID())});

    cursor.moveToFirst();
    athlete.setFirstName(cursor.getString(cursor.getColumnIndex(COL_FIRST_NAME)));
    athlete.setLastName(cursor.getString(cursor.getColumnIndex(COL_LAST_NAME)));
    athlete.setAge(cursor.getInt(cursor.getColumnIndex(COL_AGE)));
    athlete.setGrade(cursor.getString(cursor.getColumnIndex(COL_GRADE)));

     int i = 0;
    if (cursor.moveToFirst()) {
        do {
            String event = cursor.getString(cursor.getColumnIndex(COL_EVENT));
            athlete.addEvent(event);
            Log.e("", "Events: " + athlete.getEvents().get(i));
            i++;
        } while (cursor.moveToNext());
    }
    return athlete;
}

主要活动:从片段a发送到活动和活动更新片段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;
}

在创建的Activity中创建DataBase实例。这是片段。

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

获取第一条日志消息,然后说我的数据库是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());

1 个答案:

答案 0 :(得分:0)

setInterval(function(){ $('#someId').fadeIn(); }, 3000); setInterval(function(){ $('#someId').fadeOut(); }, 4000); 仅在片段生命周期的db中初始化,但您在构建片段后立即调用onActivityCreated()。生命周期方法尚未运行。

通常,不要在片段生命周期回调之外调用片段方法。要将数据从外部传递到片段,请使用addAthleteToList()Bundle。推迟需要setArguments()Context的代码,直到片段附加到生命周期中的一个Activity或更晚。

代码中还存在其他问题,例如与onAttach()进行字符串比较而不检查==的结果。