Program stopped unexpectedly when access database

by iamaelephant » Sun, 05 Oct 2008 20:11:29 GMT


Sponsored Links
 'm brand new to Android development (and mobile development in
general) so please bare with me. I'm trying to write a very simple app
that will display a list of items from a database to get myself
acquainted with the environment. Everything works fine until I try to
access the database. In my main activity class I have a method,
fillData() which creates a new cursor by calling the method
fetchAllEntries() from my database helper class. I have narrowed the
problem down to this method.

If I return null from this method, it works fine. Obviously I don't
want to return null, I want to return a Cursor. I use the following
code in the method:

String[] columns = {KEY_AUTHOR, KEY_ENTRY};
Cursor c = mDb.query(DATABASE_TABLE, columns, null, null, null, null,
null);
return c;

This code is virtually identical to the code in Google's notepad
tutorial. It should work perfectly. It doesn't - the apps just stops
responding. For completeness I'll include some other relevant code
from my database helper class. Please help me with this problem, it
has me tearing my hair out. I can't even do something so basic without
breaking this damn thing.

public class FriendBoxDbAdapter {

public static final String KEY_AUTHOR = "author";
public static final String KEY_ENTRY = "entry";
public static final String KEY_ROWID = "_id";

public static final String TAG = "FriendBoxDbAdapter";

private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;

/* Database creation SQL statement */
private static final String DATABASE_CREATE =
"create table inbox (_id integer primary key
autoincrement,
"
+ "author text not null, entry text not null);";

private static final String DATABASE_NAME = "data";
private static final String DATABASE_TABLE = "inbox";
private static final int DATABASE_VERSION = 2;

private final Context mCtx;

/**
* Inner class DatabaseHelper helps us interface with the DB.
* Pretty standard SQLite statements used to initialise/upgrade
DB.
*/
private static class DatabaseHelper extends SQLiteOpenHelper {

DatabaseHelper(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(TAG, "Upgrading database from version " + oldVersion
+ " to "
+ newVersion + ", which will destroy all old
data");
db.execSQL("DROP TABLE IF EXISTS inbox");
onCreate(db);
}
}

/**
* Finally, our constructor. Gives us the context for our DB
* to be created and managed.
* @param ctx The context we can work in
*/
public FriendBoxDbAdapter (Context ctx) {
mCtx = ctx;
}

/**
* Opens our database using Android's getWritableDatabase
* which is required to write to a DB.
* @return this (self reference allows chained initialisation
calls.
* @throws SQLException if the database can't be opened or
created.



Other Threads

1. Image buttons changing position based on orientation of phone.

I don't think it's necessary to check yourself, just provide the
landscape layout in the folder res/layout-land and leave the other one
in res/layout and the magic of Android will manage this for you.

The problem with your pictures is probably that they have the wrong
aspect ration...they are portrait pictures that you are showing in
Landscape, so they are reduced in size.

Good Luck
John





> > 

2. Simple animation question

Hello Android newbie here.

Let's say I have two Drawables and I want to swap them and animate the
swapping process.

Is there an animation class that will take care of this or do I have
to use a thread and draw each frame step-by-step?

Thanks in advance.

-- 

3. Google Experience Phone Should Be Plain Android

4. Sending raw query or SQLitedatabase.query to a database that only is reachable through ContentResolver

5. Custom address fields when switching from Palm OS to Android

6. To Install Android

7. Developers, please list changes when posting app updates to Android Market