0

Hi I've been looking how to populate a spinner from a cursor for a while and can't do it properly, this is my code:

private void fillSpinner(){

    DatabaseConnector dbConnector = new DatabaseConnector(this);
    Cursor c = dbConnector.getAllCasas();
    startManagingCursor(c);
    String[] from = new String[]{"nombrecasa"};
    int[] to = new int[]{android.R.id.text1};
    SimpleCursorAdapter adapter =
    new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to );
    adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
    Spinner s = (Spinner) findViewById( R.id.casaspinner);
    s.setAdapter(adapter);
    }

where DAtabaseConnector is the class where i got all the cursors and casaspinner is properly declarated in the XML file.

Call this method from the onCreate method but it crashes "Sorry, the application has stoppend unexpectedly" logcat shows "NullPointerException" tried some answers from here but it keeps crashing.

Here's logcat:

12-09 10:02:49.399: W/ResourceType(322): Attempt to retrieve bag 0x7f060003 which is invalid or in a cycle.
12-09 10:02:49.489: D/AndroidRuntime(322): Shutting down VM
12-09 10:02:49.489: W/dalvikvm(322): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-09 10:02:49.509: E/AndroidRuntime(322): FATAL EXCEPTION: main
12-09 10:02:49.509: E/AndroidRuntime(322): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.appvino/com.example.appvino.Actualizacion}:     java.lang.NullPointerException
12-09 10:02:49.509: E/AndroidRuntime(322):  at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-09 10:02:49.509: E/AndroidRuntime(322):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-09 10:02:49.509: E/AndroidRuntime(322):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-09 10:02:49.509: E/AndroidRuntime(322):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-09 10:02:49.509: E/AndroidRuntime(322):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-09 10:02:49.509: E/AndroidRuntime(322):  at android.os.Looper.loop(Looper.java:123)
12-09 10:02:49.509: E/AndroidRuntime(322):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-09 10:02:49.509: E/AndroidRuntime(322):  at java.lang.reflect.Method.invokeNative(Native Method)
12-09 10:02:49.509: E/AndroidRuntime(322):  at java.lang.reflect.Method.invoke(Method.java:507)
12-09 10:02:49.509: E/AndroidRuntime(322):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-09 10:02:49.509: E/AndroidRuntime(322):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-09 10:02:49.509: E/AndroidRuntime(322):  at dalvik.system.NativeStart.main(Native Method)
12-09 10:02:49.509: E/AndroidRuntime(322): Caused by: java.lang.NullPointerException
12-09 10:02:49.509: E/AndroidRuntime(322):  at com.example.appvino.DatabaseConnector.getAllCepas(DatabaseConnector.java:95)
12-09 10:02:49.509: E/AndroidRuntime(322):  at com.example.appvino.Actualizacion.llenaCepaSpinner(Actualizacion.java:129)
12-09 10:02:49.509: E/AndroidRuntime(322):  at com.example.appvino.Actualizacion.onCreate(Actualizacion.java:40)
12-09 10:02:49.509: E/AndroidRuntime(322):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-09 10:02:49.509: E/AndroidRuntime(322):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-09 10:02:49.509: E/AndroidRuntime(322):  ... 11 more
4

1 回答 1

0

我错过了可写数据库。这是解决方案:

private void llenaCasaSpinner(){

        DatabaseConnector dbConnector = new DatabaseConnector(this);
        dbConnector.open();
        Cursor c = dbConnector.raw("SELECT _id, nombrecasa FROM casa");
        startManagingCursor(c);
        String[] from = new String[]{"nombrecasa"};
        int[] to = new int[]{android.R.id.text1};
        SimpleCursorAdapter adapter =
        new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to );
        adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
        Spinner s = (Spinner) findViewById( R.id.casaspinner);
        s.setAdapter(adapter);
        dbConnector.close();
        }
于 2012-12-09T19:44:11.533 回答