0

可能重复:
系统服务在 onCreate 之前对活动不可用?

我正在尝试将数据库的值返回到列表视图,但出现错误。我正在使用游标从表中获取数据,并使用 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()"

谁能帮我这个?我找不到任何关于光标和光标适配器的好教程,我真的完全迷路了

编辑:

这可能会有所帮助。

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

模块数据源.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
4

2 回答 2

5

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

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

        }
}
于 2012-11-06T23:26:42.063 回答
0

您正在 Activity 构造函数中执行某些操作,而应该在 onCreate() 中完成。

这可能与此问题相同:系统服务在 onCreate 之前对活动不可用?

于 2012-11-06T23:12:48.833 回答