什么可能导致游标失败?

时间:2013-02-18 22:08:03

标签: android android-cursor

我正在使用Eclipse创建一个Android应用程序并尝试使用Cursor来查询SQLite数据库。我无法弄清楚导致此游标失败的原因:

public List<Word> getAllWords() {
    List<Word> words = new ArrayList<Word>(); 

    Cursor cursor = db.query(TABLE_NAME, new String[] {FIELD_PRIMARY_DEFINITION}, null, null, null, null, null);
    cursor.close();

    return words;
}

当我运行此代码时,我的Android模拟器表示该程序已停止。当我注释掉光标线时,它运行正常。数据库存在,表存在,TABLE_NAME包含表的正确名称,FIELD_PRIMARY_DEFINITION包含列的名称。有什么我想念的东西会导致失败吗?

谢谢。

我不熟悉Logcat,我是Android开发的新手。但是我对我的应用名称进行了过滤,这里显示的是:

02-18 22:26:08.447: I/jdwp(20692): Ignoring second debugger -- accepting and dropping
02-18 22:26:15.107: W/Trace(20989): Unexpected value from nativeGetEnabledTags: 0
02-18 22:26:15.148: W/Trace(20989): Unexpected value from nativeGetEnabledTags: 0
02-18 22:26:15.187: D/AndroidRuntime(20989): Shutting down VM
02-18 22:26:15.187: W/dalvikvm(20989): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
02-18 22:26:15.247: E/AndroidRuntime(20989): FATAL EXCEPTION: main
02-18 22:26:15.247: E/AndroidRuntime(20989): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.source.wordCollector/com.source.wordCollector.MainActivity}: java.lang.NullPointerException
02-18 22:26:15.247: E/AndroidRuntime(20989):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-18 22:26:15.247: E/AndroidRuntime(20989):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-18 22:26:15.247: E/AndroidRuntime(20989):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-18 22:26:15.247: E/AndroidRuntime(20989):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-18 22:26:15.247: E/AndroidRuntime(20989):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 22:26:15.247: E/AndroidRuntime(20989):    at android.os.Looper.loop(Looper.java:137)
02-18 22:26:15.247: E/AndroidRuntime(20989):    at android.app.ActivityThread.main(ActivityThread.java:5039)
02-18 22:26:15.247: E/AndroidRuntime(20989):    at java.lang.reflect.Method.invokeNative(Native Method)
02-18 22:26:15.247: E/AndroidRuntime(20989):    at java.lang.reflect.Method.invoke(Method.java:511)
02-18 22:26:15.247: E/AndroidRuntime(20989):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-18 22:26:15.247: E/AndroidRuntime(20989):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-18 22:26:15.247: E/AndroidRuntime(20989):    at dalvik.system.NativeStart.main(Native Method)
02-18 22:26:15.247: E/AndroidRuntime(20989): Caused by: java.lang.NullPointerException
02-18 22:26:15.247: E/AndroidRuntime(20989):    at com.source.dal.WordDAO.getAllWords(WordDAO.java:75)
02-18 22:26:15.247: E/AndroidRuntime(20989):    at com.source.wordCollector.MainActivity.onCreate(MainActivity.java:23)
02-18 22:26:15.247: E/AndroidRuntime(20989):    at android.app.Activity.performCreate(Activity.java:5104)
02-18 22:26:15.247: E/AndroidRuntime(20989):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-18 22:26:15.247: E/AndroidRuntime(20989):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

1 个答案:

答案 0 :(得分:0)

尝试记录并检查您正在编写的动态查询,并确保没有语法错误。大多数情况下,这可能导致安卓游标失败。

还要确保已打开与数据库的连接。我建议你看一下这篇文章:

www.vogella.com/articles/AndroidSQLite/article.html