Android 在另一个活动中访问在一个活动中创建的数据库

Android 在另一个活动中访问在一个活动中创建的数据库,android,sqlite,null,instance,Android,Sqlite,Null,Instance,出于某种奇怪的原因,我似乎无法从数据库中提取在一个活动中创建的信息。我对静态arraylist也有同样的问题。本以为使用数据库会容易得多,但它不断崩溃。我的DB实例在下一个活动中始终为空,即使我在提取任何信息之前调用了它。这是我在fragment类中的函数 private void populateListView(){ Cursor cursor = myDatabase.getAllData(); String[] fromfieldNames = new String[]{

出于某种奇怪的原因,我似乎无法从数据库中提取在一个活动中创建的信息。我对静态arraylist也有同样的问题。本以为使用数据库会容易得多,但它不断崩溃。我的DB实例在下一个活动中始终为空,即使我在提取任何信息之前调用了它。这是我在fragment类中的函数

private void populateListView(){
    Cursor cursor = myDatabase.getAllData();
    String[] fromfieldNames = new String[]{StudentDBOpenHelper.KEY_ID,StudentDBOpenHelper.ITEM_NAME_COLUMN};
    int[] toViewIDs = new int[] {R.id.textView_itemNumber,R.id.textView_itemName};
    SimpleCursorAdapter myCursorAdapter;
    myCursorAdapter = new SimpleCursorAdapter(getActivity().getBaseContext(),
            R.layout.individualview,cursor,fromfieldNames,toViewIDs,0);
    ListView myList = (ListView) getActivity().findViewById(R.id.courseListXML);
    myList.setAdapter(myCursorAdapter);
}//USed to populate list view
函数本身位于onCreateView和onViewCreated之外,但在onViewCreated中调用

这是我的数据库助手类

public class StudentDBOpenHelper extends SQLiteOpenHelper {

public static final String KEY_ID = "_id";

public static final String ITEM_NAME_COLUMN = "ITEM_NAME_COLUMN";

public static final String ITEM_CATEGORY_COLUMN = "ITEM_CATEGORY_COLUMN";

public static final String ITEM_GRADE_COLUMN = "ITEM_GRADE_COLUMN";


public static final String DATABASE_NAME = "myItemDatabase.db";
public static final String DATABASE_TABLE = "ItemInfo";
public static final int DATABASE_VERSION = 1;

private static final String DATABASE_CREATE ="create table " + DATABASE_TABLE + " (" +
        KEY_ID + " integer primary key autoincrement, " +
        ITEM_NAME_COLUMN + " text, " +
        ITEM_CATEGORY_COLUMN + " text, "
        +ITEM_GRADE_COLUMN + " integer);";




/*
public StudentDBOpenHelper(Context context,String name,
                           SQLiteDatabase.CursorFactory factory,int version)   {
 super(context,name,factory,version);



}
*/

public StudentDBOpenHelper(Context context){
    super(context, DATABASE_NAME, null, DATABASE_VERSION);


}


@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL((DATABASE_CREATE));
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    Log.w("TaskDBAdapter", "Upgrading from version" +
            oldVersion + ",which will destroy all old data");

    db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE);
    onCreate(db);

}

public boolean insertData(String firstname,String lastname,String credits){

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues contentValues = new ContentValues(); //instance of class
    contentValues.put(ITEM_NAME_COLUMN,firstname);//2 parameters(Column name in which you
    //you want to insert data
    // and second is the value itself
    contentValues.put(ITEM_CATEGORY_COLUMN, lastname);
    contentValues.put(ITEM_GRADE_COLUMN, credits);

    long result = db.insert(DATABASE_TABLE,null,contentValues);
    if(result == -1) {
        return false;
    }
    else {

        return true;
    }
}//ends insertData functin

public Cursor getAllData(){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor result = db.rawQuery("select * from " + DATABASE_TABLE,null);
    return result;
}//ends cursor


public boolean updateData(String id,String firstname,String lastname,String credits){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(KEY_ID,id);
    contentValues.put(ITEM_NAME_COLUMN,firstname);//2 parameters(Column name in which you
    //you want to insert data
    // and second is the value itself
    contentValues.put(ITEM_CATEGORY_COLUMN, lastname);
    contentValues.put(ITEM_GRADE_COLUMN, credits);

    db.update(DATABASE_TABLE,contentValues,"_id = ?",new String[]{id});

    return true;
}

public Integer deleteData(String id){
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete(DATABASE_TABLE,"_id = ?",new String[] {id});
}
}

初始化数据库的正确方法如下所示:

StudentDBOpenHelper myDatabase= new StudentDBOpenHelper(this);

如果它是一个活动,请使用
this
作为参数;如果它是一个片段,请使用
getContext()
作为参数。

给出一些初始化数据库的代码在调用其任何方法之前,您是否在下一个活动中创建了StudentDBOpenHelper的新实例?另外,你说的是“下一个活动”,但后来你说的是片段:那么,这是一个正确的活动还是片段活动?在像StudentDBOpenHelper myDatabase这样创建onCreateView和OnView之前,我在片段类内初始化了db;我的下一个活动将碎片放大我在第一个活动中将数据插入数据库,这是在下一个活动中访问数据库的正确方式吗?@Carlitos安卓系统中的数据库是持久的,这意味着一旦存储数据,它将保持存储状态,直到用户编辑/删除数据为止(如果您实施这种方法)或者直到卸载应用程序或在“设置”中清除其数据。因此,在另一个活动中创建一个新的Db实例是可行的。我尝试在另一个活动中创建一个新的Db实例,但当我尝试访问它时,它总是说数据库是空的。@Carlitos你能写出准确的消息吗?您在logcat或where中看到了吗?原因是:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.database.Cursor.com.example.carlitos.mycourseapplicationupdate.StudentDBOpenHelper.getAllData()”