0

在我的应用程序中,我有一个数据库,因为我有 1 个包含 3 列的表。我想使用该表的 id 删除表中的一行。

我正在使用这段代码:

public long DeleteStory(int rowId)
    {
        System.out.println("pos="+rowId);
        long del=db.delete(DATABASE_TABLE, KEY_ROWID + "="+rowId,null);
        System.out.println("edl="+del);
        return del;
    }

我的数据库包含数据:

id              heading                 Description
1               fgghsgahs               tyfgjhdfsdks

但它显示为空指针异常..我尝试了很多,但我无法解决它。帮我解决这个问题..提前致谢。

我的logcat如下:

12-22 10:34:50.796: I/System.out(1207): pppppppp=1
12-22 10:34:50.816: I/System.out(1207): pos=1
12-22 10:34:50.816: D/AndroidRuntime(1207): Shutting down VM
12-22 10:34:50.816: W/dalvikvm(1207): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
12-22 10:34:50.816: E/AndroidRuntime(1207): Uncaught handler: thread main exiting due to uncaught exception
12-22 10:34:50.836: E/AndroidRuntime(1207): java.lang.NullPointerException
12-22 10:34:50.836: E/AndroidRuntime(1207):     at com.newspaper.DBadapter.DeleteStory(DBadapter.java:89)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at com.newspaper.SavedStoriesActivity.onOptionsItemSelected(SavedStoriesActivity.java:64)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at android.app.Activity.onMenuItemSelected(Activity.java:2170)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:730)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:139)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:525)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at android.view.View.onTouchEvent(View.java:4179)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at android.widget.TextView.onTouchEvent(TextView.java:6541)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at android.view.View.dispatchTouchEvent(View.java:3709)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at android.os.Looper.loop(Looper.java:123)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at android.app.ActivityThread.main(ActivityThread.java:4363)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at java.lang.reflect.Method.invokeNative(Native Method)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at java.lang.reflect.Method.invoke(Method.java:521)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-22 10:34:50.836: E/AndroidRuntime(1207):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

1

LogCat 显示此行正在引发 NullPointerException:

long del=db.delete(DATABASE_TABLE, KEY_ROWID + "="+rowId,null);

这意味着dbnullopen()您的数据库适配器中有方法吗?你需要调用getWritableDatabase()你的 SQLiteOpenHelper 对象来初始化db

于 2012-12-22T05:08:28.583 回答
1

您忘记在进行数据库操作之前初始化 db 所以将您的代码更改为:

public long DeleteStory(int rowId)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        System.out.println("pos="+rowId);
        long del=db.delete(DATABASE_TABLE, KEY_ROWID + "="+rowId,null);
        System.out.println("edl="+del);
         db.close();
        return del;
    }

在数据库中删除、插入、更新操作的更多帮助:

http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

于 2012-12-22T05:09:30.583 回答