尝试将数据从sqlite添加到微调器时获取空指针异常

时间:2014-05-07 15:18:08

标签: android sqlite android-fragments

我试图从我的sqlite数据库中获取一列数据,并在我的微调器中插入每一行作为不同的标题,但我一直得到空指针异常:

来自我的sqlite适配器的代码:

public List<String> getAllTitles() {
        String[] columns = new String[] { KEY_TITLE};
        List<String> leventList = new ArrayList<String>();
                Cursor titlecursor = MainDatabase.query(DATABASE_TABLE, columns, null, null, null,
                null, null);// null pointer on this cursor.
                if (titlecursor.moveToFirst()) {
                    do {
                        leventList.add(titlecursor.getString(1));
                    } while (titlecursor.moveToNext());
                }
                return leventList;

    }

我的onStart()中的代码:

    mspinner = (Spinner)getView().findViewById(R.id.spinner1);
            mdatabase = new MainListSQLiteAdapter(getActivity());
mdatabase.open();
            List<String>lables = mdatabase.getAllTitles();
            ArrayAdapter<String> uadapter = new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_spinner_item, lables);
            uadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            mspinner.setAdapter(uadapter);
mdatabase.close();

我很乐意填充sqlite数据库。

编辑(DATABASE OPENED) logcat:

05-07 11:26:37.798: E/AndroidRuntime(5579): FATAL EXCEPTION: main
05-07 11:26:37.798: E/AndroidRuntime(5579): java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
05-07 11:26:37.798: E/AndroidRuntime(5579):     at android.database.CursorWindow.nativeGetString(Native Method)
05-07 11:26:37.798: E/AndroidRuntime(5579):     at android.database.CursorWindow.getString(CursorWindow.java:434)
05-07 11:26:37.798: E/AndroidRuntime(5579):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
05-07 11:26:37.798: E/AndroidRuntime(5579):     at com.wordpress.yourhappening.happening.MainListSQLiteAdapter.getAllTitles(MainListSQLiteAdapter.java:285)
05-07 11:26:37.798: E/AndroidRuntime(5579):     at com.wordpress.yourhappening.happening.Closest.onStart(Closest.java:57)

1 个答案:

答案 0 :(得分:0)

更改此行

leventList.add(titlecursor.getString(1));

到这个

leventList.add(titlecursor.getString(0));