无法在微调器上显示数据?抛出空指针异常

发布于 2024-12-03 21:09:11 字数 3356 浏览 0 评论 0原文

谁能告诉我这段代码中有什么错误吗...我把代码放在下面。

mySQLiteAdapter = new SQLiteAdapter(this);
    cursor = mySQLiteAdapter.displayCategory();
    startManagingCursor(cursor);

    String[] from = new String[] { SQLiteAdapter.KEY_ID, SQLiteAdapter.KEY_CATEGORY };

    int[] to = new int[] { android.R.id.text1 };

    mCategory = (Spinner) findViewById(R.id.choose_category);
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            android.R.layout.simple_spinner_item, cursor, from, to);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mCategory.setAdapter(adapter);

MyAdapter 类具有以下代码

public Cursor displayCategory() {
        String[] columns = new String[]{KEY_ID, KEY_CATEGORY};          
        Cursor cursor = sqLiteDatabase.query(MYCATEGORY_TABLE, columns, 
                null, null, null, null, null);

        return cursor;

空指针异常。如何避免呢。 这是 log cat 上的错误

 09-09 15:49:16.535: ERROR/AndroidRuntime(6526): Uncaught handler: thread main exiting  due to uncaught exception
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.mpt.receiptor/com.android.mpt.receiptor.SearchReceipt}: java.lang.NullPointerException
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at android.os.Looper.loop(Looper.java:123)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at android.app.ActivityThread.main(ActivityThread.java:4363)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at java.lang.reflect.Method.invokeNative(Native Method)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at java.lang.reflect.Method.invoke(Method.java:521)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at dalvik.system.NativeStart.main(Native Method)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): Caused by: java.lang.NullPointerException
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at com.android.mpt.receiptor.SQLiteAdapter.displayCategory(SQLiteAdapter.java:135)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at com.android.mpt.receiptor.SearchReceipt.onCreate(SearchReceipt.java:96)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)

请有人帮助我。

提前致谢

can anyone tell wats wrong in this code... I put code bellow.

mySQLiteAdapter = new SQLiteAdapter(this);
    cursor = mySQLiteAdapter.displayCategory();
    startManagingCursor(cursor);

    String[] from = new String[] { SQLiteAdapter.KEY_ID, SQLiteAdapter.KEY_CATEGORY };

    int[] to = new int[] { android.R.id.text1 };

    mCategory = (Spinner) findViewById(R.id.choose_category);
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            android.R.layout.simple_spinner_item, cursor, from, to);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mCategory.setAdapter(adapter);

MyAdapter class having following code

public Cursor displayCategory() {
        String[] columns = new String[]{KEY_ID, KEY_CATEGORY};          
        Cursor cursor = sqLiteDatabase.query(MYCATEGORY_TABLE, columns, 
                null, null, null, null, null);

        return cursor;

Null pointer Exception. How to avoid it.
This is the error on log cat

 09-09 15:49:16.535: ERROR/AndroidRuntime(6526): Uncaught handler: thread main exiting  due to uncaught exception
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.mpt.receiptor/com.android.mpt.receiptor.SearchReceipt}: java.lang.NullPointerException
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at android.os.Looper.loop(Looper.java:123)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at android.app.ActivityThread.main(ActivityThread.java:4363)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at java.lang.reflect.Method.invokeNative(Native Method)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at java.lang.reflect.Method.invoke(Method.java:521)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at dalvik.system.NativeStart.main(Native Method)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): Caused by: java.lang.NullPointerException
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at com.android.mpt.receiptor.SQLiteAdapter.displayCategory(SQLiteAdapter.java:135)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at com.android.mpt.receiptor.SearchReceipt.onCreate(SearchReceipt.java:96)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)

Pls someone help me.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜无邪 2024-12-10 21:09:11

它看起来像这一行:

Cursor cursor = sqLiteDatabase.query(MYCATEGORY_TABLE, columns,  
            null, null, null, null, null); 

sqLiteDatabase 为 null - 确保它在您的 onCreate 方法中初始化。

It looks like in this line:

Cursor cursor = sqLiteDatabase.query(MYCATEGORY_TABLE, columns,  
            null, null, null, null, null); 

that sqLiteDatabase is null - make sure it is initialised in your onCreate method.

有深☉意 2024-12-10 21:09:11

为什么不能在 displayCategory() 方法中放置断点?空指针异常很容易找到。正如 Cjk 所指出的,sqlitedatabase 是统一的。调试将帮助您在浪费大量时间之前发现并解决问题。

why cant you put a breakpoint in the displayCategory() method? null pointer exceptions are quite easy to find.As Cjk has pointed out, sqlitedatabase is unitialised. Debugging will help you find and solve issues before you lose much of your time..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文