Possible Duplicate:
System services not available to Activities before onCreate?
I am trying to return the values of a database to a listview but I am getting an error. I am using a cursor to get the data from a table and a SimpleCursorAdapter to read the cursor. This is the method that does this:
public void getData(){
String[] columns = new String[] {ModuleDB.KEY_ROWID,
ModuleDB.KEY_MODCODE,
ModuleDB.KEY_MODNAME,
ModuleDB.KEY_LECPRAC,
ModuleDB.KEY_DAYS,
ModuleDB.KEY_TIMESTART,
ModuleDB.KEY_TIMEEND,
ModuleDB.KEY_LOCATION,
ModuleDB.KEY_INFO};
int[] to = new int[]{R.id.modcode_entry,
R.id.modname_entry,
R.id.modlecprac_entry,
R.id.modday_entry,
R.id.modtimestart_entry,
R.id.modtimeend_entry,
R.id.modlocation_entry,
R.id.modaddinfo_entry};
Cursor curs = myDatabase.query(ModuleDB.DB_TABLE, columns, null, null, null, null, null);
if (curs != null)
curs.moveToFirst();
SimpleCursorAdapter dataSource = new SimpleCursorAdapter(ModuleDataSource.this, R.layout.list_entry, curs, columns, to);
this.setListAdapter(dataSource);
// Make sure to close the cursor
curs.close();
}
This method is causing the exception: "System Services not available to activites before onCreate()"
Can anyone help me with this? I can’t find any good tutorials for cursors and cursoradapters and I really am completely lost
EDIT:
This might help.
MainActivity.java
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ModuleDataSource dbinfo = new ModuleDataSource(this);
dbinfo.open();
dbinfo.getData();
dbinfo.close();
}
ModuleDataSource.java
public class ModuleDataSource extends ListActivity{
private SQLiteDatabase myDatabase;
private ModuleDB myhelper;
public ModuleDataSource(Context context){
myhelper = new ModuleDB(context);
}
public void open() throws SQLException { //Open the database for writing
myDatabase = myhelper.getWritableDatabase();
}
public void close(){ //Close the database
myhelper.close();
}
public void getData(){
String[] columns = new String[] {ModuleDB.KEY_ROWID,
ModuleDB.KEY_MODCODE,
ModuleDB.KEY_MODNAME,
ModuleDB.KEY_LECPRAC,
ModuleDB.KEY_DAYS,
ModuleDB.KEY_TIMESTART,
ModuleDB.KEY_TIMEEND,
ModuleDB.KEY_LOCATION,
ModuleDB.KEY_INFO};
int[] to = new int[]{R.id.modcode_entry,
R.id.modname_entry,
R.id.modlecprac_entry,
R.id.modday_entry,
R.id.modtimestart_entry,
R.id.modtimeend_entry,
R.id.modlocation_entry,
R.id.modaddinfo_entry};
Cursor curs = myDatabase.query(ModuleDB.DB_TABLE, columns, null, null, null, null, null);
startManagingCursor(curs);
if (curs != null)
curs.moveToFirst();
SimpleCursorAdapter dataSource = new SimpleCursorAdapter(ModuleDataSource.this, R.layout.list_entry, curs, columns, to);
setListAdapter(dataSource);
}
}
EDIT2: Logcat
11-06 23:24:07.885: D/AndroidRuntime(887): Shutting down VM
11-06 23:24:07.885: W/dalvikvm(887): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-06 23:24:07.895: E/AndroidRuntime(887): FATAL EXCEPTION: main
11-06 23:24:07.895: E/AndroidRuntime(887): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.adoherty.assignment3/com.adoherty.assignment3.MainActivity}:
java.lang.IllegalStateException: System services not available to Activities before onCreate()
11-06 23:24:07.895: E/AndroidRuntime(887): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-06 23:24:07.895: E/AndroidRuntime(887): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-06 23:24:07.895: E/AndroidRuntime(887): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-06 23:24:07.895: E/AndroidRuntime(887): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-06 23:24:07.895: E/AndroidRuntime(887): at android.os.Handler.dispatchMessage(Handler.java:99)
11-06 23:24:07.895: E/AndroidRuntime(887): at android.os.Looper.loop(Looper.java:123)
11-06 23:24:07.895: E/AndroidRuntime(887): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-06 23:24:07.895: E/AndroidRuntime(887): at java.lang.reflect.Method.invokeNative(Native Method)
11-06 23:24:07.895: E/AndroidRuntime(887): at java.lang.reflect.Method.invoke(Method.java:521)
11-06 23:24:07.895: E/AndroidRuntime(887): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-06 23:24:07.895: E/AndroidRuntime(887): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-06 23:24:07.895: E/AndroidRuntime(887): at dalvik.system.NativeStart.main(Native Method)
11-06 23:24:07.895: E/AndroidRuntime(887): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
11-06 23:24:07.895: E/AndroidRuntime(887): at android.app.Activity.getSystemService(Activity.java:3526)
11-06 23:24:07.895: E/AndroidRuntime(887): at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:49)
11-06 23:24:07.895: E/AndroidRuntime(887): at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:84)
11-06 23:24:07.895: E/AndroidRuntime(887): at com.adoherty.assignment3.ModuleDataSource.getData(ModuleDataSource.java:74)
11-06 23:24:07.895: E/AndroidRuntime(887): at com.adoherty.assignment3.MainActivity.onCreate(MainActivity.java:17)
11-06 23:24:07.895: E/AndroidRuntime(887): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-06 23:24:07.895: E/AndroidRuntime(887): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-06 23:24:07.895: E/AndroidRuntime(887): ... 11 more
ModuleDataSource extends ListActivity.. you are creating an instance of that within your Activity.. You cannot instantiate an activity as an object like thatYour ListActivity is likely trying to make some system call that it normally makes in the background automatically for you. Since you are trying to instantiate a ListActivity as an object, the normal Activity process is not happening…
You should move what you have in the ListActivity into your main activity and make your main activity the ListActivity
Something like this: