Cursor和SimpleCursorAdapter

时间:2012-11-06 22:53:58

标签: android sqlite simplecursoradapter

  

可能重复:
  System services not available to Activities before onCreate?

我正在尝试将数据库的值返回到listview但是我收到错误。我使用游标从表中获取数据,使用SimpleCursorAdapter读取游标。这是执行此操作的方法:

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();
        }

此方法导致异常:"System Services not available to activites before onCreate()"

任何人都可以帮我吗?我找不到任何关于游标和cursoradapter的好教程,我真的完全迷失了

编辑:

这可能会有所帮助。

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

2 个答案:

答案 0 :(得分:5)

ModuleDataSource extends ListActivity ..您正在Activity中创建该实例..您不能将活动实例化为像这样的对象

您的ListActivity可能会尝试进行一些通常在后台自动为您进行的系统调用。由于您尝试将ListActivity实例化为对象,因此不会发生正常的Activity过程......

您应该将ListActivity中的内容移动到主活动中,并将主要活动设为ListActivity

这样的事情:

public class MainActivity extends ListActivity {

    private SQLiteDatabase myDatabase;
    private ModuleDB myhelper;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myhelper = new ModuleDB(this);
        open();
        getData();
        close();
    }

    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);

        SimpleCursorAdapter dataSource = new SimpleCursorAdapter(ModuleDataSource.this, R.layout.list_entry, curs, columns, to);
        setListAdapter(dataSource);

        }
}

答案 1 :(得分:0)

你在Activity构造函数中做了一些事情,应该在onCreate()中完成。

这可能与此问题相同:System services not available to Activities before onCreate?