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. How I get values from a ListPreference??

Hi!
I'm new in this group and developing Android as well.

I'm working in a PreferenceActivity with a ListPreference.

I created the xml and array.xml well.

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
  xmlns:android="http://schemas.android.com/apk/res/android">
        <ListPreference
                android:key="denbora_3g"
                android:title="@string/denbora_label"
                android:summary="@string/denbora_summary"
                android:entries="@array/denbora"
                android:entryValues="@array/denbora_values"
                android:defaultValue="10000"
        />
</PreferenceScreen>

<?xml version="1.0" encoding="utf-8"?>
<resources>
        <array name="denbora">
                <item>@string/denbora_10</item>
                <item>@string/denbora_15</item>
                <item>@string/denbora_20</item>
                <item>@string/denbora_25</item>
        </array>
        <array name="denbora_values">
                <item>10000</item>
                <item>15000</item>
                <item>20000</item>
                <item>25000</item>
        </array>
</resources>

Now I want to get the value clicked in the ListPreference, in the java
class.

How can I do it?

Thanks a lot!

--

2. Salam kenal nubie

Salam kenal suhu2 sekalian, nubie beli nexus one masih standard belum diapa2in, 
mohon pencerahannya dari para suhu, buat mod thx sudah di approve..
Regards,

-- 
"Indonesian Android Community [id-android]" 

3. How do i create an icon on the status bar?

4. IllegalArgumentException occurs when passing url to post data ... plz help

5. (WTA) Tsel flash unlimited 125k dan 225k

6. Use soft keyboard all the time

7. (WTA) Setiap buka messaging minta force close?