creating a Menu from SQLite values in Java
- by shanahobo86
I am trying to create a ListMenu using data from an SQLite database to define the name of each MenuItem. So in a class called menu.java I have defined the array
String classes [] = {};
which should hold each menu item name.
In a DBAdapter class I created a function so the user can insert info to a table (This all works fine btw).
public long insertContact(String name, String code, String location, String comments, int days, int start, int end, String type)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_CODE, code);
initialValues.put(KEY_LOCATION, location);
initialValues.put(KEY_COMMENTS, comments);
initialValues.put(KEY_DAYS, days);
initialValues.put(KEY_START, start);
initialValues.put(KEY_END, end);
initialValues.put(KEY_TYPE, type);
return db.insert(DATABASE_TABLE, null, initialValues);
}
It would be the Strings inserted into KEY_NAME that I need to populate that String array with. Does anyone know if this is possible?
Thanks so much for the help guys.
If I implement that function by Sam/Mango the program crashes, am I using it incorrectly or is the error due to the unknown size of the array?
DBAdapter db = new DBAdapter(this);
String classes [] = db.getClasses();
edit: I should mention that if I manually define the array:
String classes [] = {"test1", "test2", "test3", etc};
It works fine. The error is a NullPointerException
Here's the logcat (sorry about the formatting). I hadn't initialized with db = helper.getReadableDatabase(); in the getClasses() function but unfortunately it didn't fix the problem.
11-11 22:53:39.117: D/dalvikvm(17856): Late-enabling CheckJNI
11-11 22:53:39.297: D/TextLayoutCache(17856): Using debug level: 0 - Debug Enabled: 0
11-11 22:53:39.337: D/libEGL(17856): loaded /system/lib/egl/libGLES_android.so
11-11 22:53:39.337: D/libEGL(17856): loaded /system/lib/egl/libEGL_adreno200.so
11-11 22:53:39.357: D/libEGL(17856): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
11-11 22:53:39.357: D/libEGL(17856): loaded /system/lib/egl/libGLESv2_adreno200.so
11-11 22:53:39.387: I/Adreno200-EGLSUB(17856): <ConfigWindowMatch:2078>: Format RGBA_8888.
11-11 22:53:39.407: D/memalloc(17856): /dev/pmem: Mapped buffer base:0x5c66d000 size:36593664 offset:32825344 fd:65
11-11 22:53:39.417: E/(17856): Can't open file for reading
11-11 22:53:39.417: E/(17856): Can't open file for reading
11-11 22:53:39.417: D/OpenGLRenderer(17856): Enabling debug mode 0
11-11 22:53:39.477: D/memalloc(17856): /dev/pmem: Mapped buffer base:0x5ecd3000 size:40361984 offset:36593664 fd:68
11-11 22:53:40.507: D/memalloc(17856): /dev/pmem: Mapped buffer base:0x61451000 size:7254016 offset:3485696 fd:71
11-11 22:53:41.077: I/Adreno200-EGLSUB(17856): <ConfigWindowMatch:2078>: Format RGBA_8888.
11-11 22:53:41.077: D/memalloc(17856): /dev/pmem: Mapped buffer base:0x61c4c000 size:7725056 offset:7254016 fd:74
11-11 22:53:41.097: D/memalloc(17856): /dev/pmem: Mapped buffer base:0x623aa000 size:8196096 offset:7725056 fd:80
11-11 22:53:41.937: D/memalloc(17856): /dev/pmem: Mapped buffer base:0x62b7b000 size:8667136 offset:8196096 fd:83
11-11 22:53:41.977: D/memalloc(17856): /dev/pmem: Unmapping buffer base:0x61c4c000 size:7725056 offset:7254016
11-11 22:53:41.977: D/memalloc(17856): /dev/pmem: Unmapping buffer base:0x623aa000 size:8196096 offset:7725056
11-11 22:53:41.977: D/memalloc(17856): /dev/pmem: Unmapping buffer base:0x62b7b000 size:8667136 offset:8196096
11-11 22:53:42.167: I/Adreno200-EGLSUB(17856): <ConfigWindowMatch:2078>: Format RGBA_8888.
11-11 22:53:42.177: D/memalloc(17856): /dev/pmem: Mapped buffer base:0x61c5d000 size:17084416 offset:13316096 fd:74
11-11 22:53:42.317: D/memalloc(17856): /dev/pmem: Mapped buffer base:0x63853000 size:20852736 offset:17084416 fd:80
11-11 22:53:42.357: D/OpenGLRenderer(17856): Flushing caches (mode 0)
11-11 22:53:42.357: D/memalloc(17856): /dev/pmem: Unmapping buffer base:0x5c66d000 size:36593664 offset:32825344
11-11 22:53:42.357: D/memalloc(17856): /dev/pmem: Unmapping buffer base:0x5ecd3000 size:40361984 offset:36593664
11-11 22:53:42.367: D/memalloc(17856): /dev/pmem: Unmapping buffer base:0x61451000 size:7254016 offset:3485696
11-11 22:53:42.757: D/memalloc(17856): /dev/pmem: Mapped buffer base:0x5c56d000 size:24621056 offset:20852736 fd:65
11-11 22:53:44.247: D/AndroidRuntime(17856): Shutting down VM
11-11 22:53:44.247: W/dalvikvm(17856): threadid=1: thread exiting with uncaught exception (group=0x40ac3210)
11-11 22:53:44.257: E/AndroidRuntime(17856): FATAL EXCEPTION: main
11-11 22:53:44.257: E/AndroidRuntime(17856): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{niall.shannon.timetable/niall.shannon.timetable.menu}: java.lang.NullPointerException
11-11 22:53:44.257: E/AndroidRuntime(17856): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1891)
11-11 22:53:44.257: E/AndroidRuntime(17856): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
11-11 22:53:44.257: E/AndroidRuntime(17856): at android.app.ActivityThread.access$600(ActivityThread.java:127)
11-11 22:53:44.257: E/AndroidRuntime(17856): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
11-11 22:53:44.257: E/AndroidRuntime(17856): at android.os.Handler.dispatchMessage(Handler.java:99)
11-11 22:53:44.257: E/AndroidRuntime(17856): at android.os.Looper.loop(Looper.java:137)
11-11 22:53:44.257: E/AndroidRuntime(17856): at android.app.ActivityThread.main(ActivityThread.java:4441)
11-11 22:53:44.257: E/AndroidRuntime(17856): at java.lang.reflect.Method.invokeNative(Native Method)
11-11 22:53:44.257: E/AndroidRuntime(17856): at java.lang.reflect.Method.invoke(Method.java:511)
11-11 22:53:44.257: E/AndroidRuntime(17856): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
11-11 22:53:44.257: E/AndroidRuntime(17856): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
11-11 22:53:44.257: E/AndroidRuntime(17856): at dalvik.system.NativeStart.main(Native Method)
11-11 22:53:44.257: E/AndroidRuntime(17856): Caused by: java.lang.NullPointerException
11-11 22:53:44.257: E/AndroidRuntime(17856): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
11-11 22:53:44.257: E/AndroidRuntime(17856): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
11-11 22:53:44.257: E/AndroidRuntime(17856): at niall.shannon.timetable.DBAdapter.getClasses(DBAdapter.java:151)
11-11 22:53:44.257: E/AndroidRuntime(17856): at niall.shannon.timetable.menu.<init>(menu.java:15)
11-11 22:53:44.257: E/AndroidRuntime(17856): at java.lang.Class.newInstanceImpl(Native Method)
11-11 22:53:44.257: E/AndroidRuntime(17856): at java.lang.Class.newInstance(Class.java:1319)
11-11 22:53:44.257: E/AndroidRuntime(17856): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
11-11 22:53:44.257: E/AndroidRuntime(17856): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1882)
11-11 22:53:44.257: E/AndroidRuntime(17856): ... 11 more
11-11 22:53:46.527: I/Process(17856): Sending signal. PID: 17856 SIG: 9