У меня есть эта проблема.У меня есть ListActivity, в котором я хочу внести все записи моей базы данных, которые впоследствии я хочу видеть в своем списке.
Теперь есть проблема исключения Nullpointer, когда я получаю все данные из моей таблицы.Вы видите это на шаге 2.
Вот код ListActivity:
package de.retowaelchli.filterit.stats;
import de.retowaelchli.filterit.database.ADFilterDBAdapter;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class CreatedADFilters extends ListActivity {
//Variablen deklaration
private ADFilterDBAdapter mDbHelper;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//Hier wir die Datenbank aufgerufen
mDbHelper = new ADFilterDBAdapter(this);
mDbHelper.open();
System.out.println("STEP1"); //Until here it works
Cursor c = mDbHelper.getAllADFilter();
startManagingCursor(c);
System.out.println("STEP2"); //Here's the problem I don't get.
}
}
Итак, вот кодовая часть моего DB-адаптера:
public Cursor getAllADFilter() {
return this.mDb.query(DATABASE_TABLE, new String[] { ROW_ID,
NAME, KEYWORD, CACHE }, null, null, null, null, null);
}
Я не понимаю, что я делаю неправильно, пожалуйста, дай мне несколько советов.
Заранее спасибо!
Итак, вот весь DB-адаптер для этой таблицы:
package de.retowaelchli.filterit.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class ADFilterDBAdapter {
public static final String ROW_ID = "_id";
public static final String NAME = "name";
public static final String KEYWORD = "keyword";
public static final String CACHE = "cache";
private static final String DATABASE_TABLE = "adfilter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DBAdapter.DATABASE_NAME, null, DBAdapter.DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
/**
* Constructor - takes the context to allow the database to be
* opened/created
*
* @param ctx
* the Context within which to work
*/
public ADFilterDBAdapter(Context ctx) {
this.mCtx = ctx;
}
/**
* Open the ADFilter database. If it cannot be opened, try to create a new
* instance of the database. If it cannot be created, throw an exception to
* signal the failure
*
* @return this (self reference, allowing this to be chained in an
* initialization call)
* @throws SQLException
* if the database could be neither opened or created
*/
public ADFilterDBAdapter open() throws SQLException {
this.mDbHelper = new DatabaseHelper(this.mCtx);
this.mDb = this.mDbHelper.getWritableDatabase();
return this;
}
/**
* close return type: void
*/
public void close() {
this.mDbHelper.close();
}
/**
* Create a new ADFilter. If the car is successfully created return the new
* rowId for that ADFilter, otherwise return a -1 to indicate failure.
*
* @param name
* @param keyword
* @param cache
* @return rowId or -1 if failed
*/
public long createADFilter(String name, String keyword, String cache){
ContentValues initialValues = new ContentValues();
initialValues.put(NAME, name);
initialValues.put(KEYWORD, keyword);
initialValues.put(CACHE, cache);
return this.mDb.insert(DATABASE_TABLE, null, initialValues);
}
/**
* Delete the ADFilter with the given rowId
*
* @param rowId
* @return true if deleted, false otherwise
*/
public boolean deleteADFilter(long rowId) {
return this.mDb.delete(DATABASE_TABLE, ROW_ID + "=" + rowId, null) > 0; //$NON-NLS-1$
}
/**
* Return a Cursor over the list of all ADFilter in the database
*
* @return Cursor over all ADFilters
*/
public Cursor getAllADFilter() {
return this.mDb.query(DATABASE_TABLE, new String[] { ROW_ID,
NAME, KEYWORD, CACHE }, null, null, null, null, null);
}
/**
* Return a Cursor positioned at the ADFilter that matches the given rowId
* @param rowId
* @return Cursor positioned to matching ADFilter, if found
* @throws SQLException if ADFilter could not be found/retrieved
*/
public Cursor getADFilter(long rowId) throws SQLException {
Cursor mCursor =
this.mDb.query(true, DATABASE_TABLE, new String[] { ROW_ID, NAME,
KEYWORD, CACHE}, ROW_ID + "=" + rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
/**
* Update the ADFilter
*
* @param rowId
* @param name
* @param keyword
* @param cache
* @return true if the note was successfully updated, false otherwise
*/
public boolean updateADFilter(long rowId, String name, String keyword,
String cache){
ContentValues args = new ContentValues();
args.put(NAME, name);
args.put(KEYWORD, keyword);
args.put(CACHE, cache);
return this.mDb.update(DATABASE_TABLE, args, ROW_ID + "=" + rowId, null) >0;
}
}
А вот и глобальный DB-адаптер:
package de.retowaelchli.filterit.database;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBAdapter {
public static final String DATABASE_NAME = "filterit"; //$NON-NLS-1$
public static final int DATABASE_VERSION = 1;
public static final String CREATE_TABLE_ADFILTER =
"create table adfilter (_id integer primary key autoincrement, " //$NON-NLS-1$
+ ADFilterDBAdapter.NAME+ " TEXT," //$NON-NLS-1$
+ ADFilterDBAdapter.KEYWORD+ " TEXT," //$NON-NLS-1$
+ ADFilterDBAdapter.CACHE+ " TEXT" + ");"; //$NON-NLS-1$ //$NON-NLS-2$
private static final String CREATE_TABLE_SFILTER = "create table sfilter (_id integer primary key autoincrement, " //$NON-NLS-1$
+SFilterDBAdapter.NAME+" TEXT," //$NON-NLS-1$
+SFilterDBAdapter.KEYWORD+" TEXT," //$NON-NLS-1$
+SFilterDBAdapter.COLOR+" TEXT,"//$NON-NLS-1$ //$NON-NLS-2$
+SFilterDBAdapter.SMILEY+" TEXT"+ ");";
private static final String CREATE_TABLE_ADMESSAGES = "create table admessages (_id integer primary key autoincrement, " //$NON-NLS-1$
+ADMessagesDBAdapter.PHONENUMBER+" TEXT," //$NON-NLS-1$
+ADMessagesDBAdapter.MESSAGE+" TEXT,"+ ");"; //$NON-NLS-1$ //$NON-NLS-2$
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
/**
* Constructor
* @param ctx
*/
public DBAdapter(Context ctx)
{
this.context = ctx;
this.DBHelper = new DatabaseHelper(this.context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(CREATE_TABLE_ADFILTER);
db.execSQL(CREATE_TABLE_SFILTER);
db.execSQL(CREATE_TABLE_ADMESSAGES);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion)
{
// Adding any table mods to this guy here
}
}
/**
* open the db
* @return this
* @throws SQLException
* return type: DBAdapter
*/
public DBAdapter open() throws SQLException
{
this.db = this.DBHelper.getWritableDatabase();
return this;
}
/**
* close the db
* return type: void
*/
public void close()
{
this.DBHelper.close();
}
}
Надеюсь, это поможет вам .. =)