【问题标题】:Accessing Database created in one activity in another Activity访问在另一个活动中的一个活动中创建的数据库
【发布时间】:2016-05-04 15:02:18
【问题描述】:

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

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 的新实例?另外,您说“下一个活动”,但稍后您会说片段:那么,它是正确的活动还是片段活动?
  • 我在 onCreateView 和 onViewCreated 之前在 Fragment 类中初始化了数据库,就像这样 StudentDBOpenHelper myDatabase;
  • 我的下一个活动膨胀片段

标签: android sqlite null instance


【解决方案1】:

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

StudentDBOpenHelper myDatabase= new StudentDBOpenHelper(this);

如果是 Activity,则使用 this 作为参数,如果是 Fragment,则使用 getContext()

【讨论】:

  • 我在第一个活动中将数据插入到数据库中,这是在下一个活动中访问数据库的正确方法吗?
  • @Carlitos Android 中的数据库是持久的,这意味着一旦存储数据,它将一直存储到用户编辑/删除数据(如果您实施此类方法)或直到应用程序被卸载或它的数据在设置中被清除。因此,在另一个活动中创建一个新的 Db 实例是可行的方法。
  • 我尝试在另一个活动中创建一个新的数据库实例,但是当我尝试访问它时它一直说数据库是空的。
  • @Carlitos 你能写出准确的信息吗?你是在 logcat 中看到的还是在哪里看到的?
  • 引起:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'android.database.Cursor com.example.carlitos.mycourseapplicationupdate.StudentDBOpenHelper.getAllData()'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-19
  • 2017-07-19
  • 2016-08-08
  • 1970-01-01
  • 2011-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多