帮助显示有关微调器的数据

时间:2010-08-02 03:50:37

标签: android database eclipse sqlite spinner

我想将数据库中的数据显示到微调器上。怎么可能呢? 因为我的logcat中存在空错误。

Booking.java

public class Booking extends Activity 
{
    private DBAdapter db; 
    private Spinner colourSpinner;
    private String txtArrival;
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        db = new DBAdapter(this);
        db.open();
        setContentView(R.layout.booking);
        Cursor c = db.getSpinnerData();
        startManagingCursor(c);
        String[] from = new String[]{txtArrival};
        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 colourSpinner = (Spinner) findViewById( R.id.spinner );
        colourSpinner.setAdapter(adapter);
        }
}

DBAdapter.java

public Cursor getSpinnerData() throws SQLException 
        {
            Cursor mCursor =
                    db.query(DATABASE_TABLE_2, new String[] {  
                            KEY_ROWID2,
                            KEY_ARRIVAL,}, 
                            null,
                            null, 
                            null, 
                            null, 
                            null);
            if (mCursor != null) {
                mCursor.moveToFirst();
            }
            return mCursor;
        }

logcat的

08-02 08:39:27.123: ERROR/AndroidRuntime(746): Uncaught handler: thread main exiting due to uncaught exception
08-02 08:39:27.174: ERROR/AndroidRuntime(746): java.lang.RuntimeException: Unable to start activity ComponentInfo{one.two/one.two.Booking}: java.lang.NullPointerException
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at android.app.ActivityThread.access$1800(ActivityThread.java:112)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at android.os.Looper.loop(Looper.java:123)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at android.app.ActivityThread.main(ActivityThread.java:3948)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at java.lang.reflect.Method.invokeNative(Native Method)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at java.lang.reflect.Method.invoke(Method.java:521)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at dalvik.system.NativeStart.main(Native Method)
08-02 08:39:27.174: ERROR/AndroidRuntime(746): Caused by: java.lang.NullPointerException
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at android.database.sqlite.SQLiteCursor.getColumnIndex(SQLiteCursor.java:311)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:312)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at android.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapter.java:336)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:88)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at one.two.Booking.onCreate(Booking.java:27)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
08-02 08:39:27.174: ERROR/AndroidRuntime(746):     ... 11 more

谢谢。

1 个答案:

答案 0 :(得分:2)

您收到NullPointerException,因为您向null提供了SimpleCursorAdapter

String[] from = new String[]{txtArrival};

这需要像:

String[] from = new String[]{KEY_ARRIVAL};

from参数的用途是指示Cursor中的哪些列应映射到TextView参数标识的to小部件。