1

我想从我的数据库处理程序文件中使用下面给定的删除功能。当我实现它时,javaNullpointerException 即将到来。我肯定错过了一些东西。

删除功能:

// Deleting single contact
public void deleteContact(Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_CONTACTS, KEY_ID + " = ?",new String[] { String.valueOf(contact.getID()) });
    db.close();
}

我正在尝试用作:

     DatabaseHandler db = new DatabaseHandler(this);
Contact pm = null;
Intent i;
i = getIntent();
pm._id= i.getExtras().getInt("id");
pm._name = i.getExtras().getString("name");
pm._phone_number = i.getExtras().getString("phone_number");

        // **Some problem is here I am not getting it

                        db.deleteContact(pm);
                        Toast.makeText(getApplicationContext(),"Deleted..",Toast.LENGTH_SHORT).show();

日志猫:

08-19 01:15:00.726: E/AndroidRuntime(12657): FATAL EXCEPTION: main
08-19 01:15:00.726: E/AndroidRuntime(12657): java.lang.NullPointerException
08-19 01:15:00.726: E/AndroidRuntime(12657):    at com.parth.targetthebudget.ViewContact$ListAdapter$2$1.onClick(ViewContact.java:151)
08-19 01:15:00.726: E/AndroidRuntime(12657):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163)
08-19 01:15:00.726: E/AndroidRuntime(12657):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-19 01:15:00.726: E/AndroidRuntime(12657):    at android.os.Looper.loop(Looper.java:130)
08-19 01:15:00.726: E/AndroidRuntime(12657):    at android.app.ActivityThread.main(ActivityThread.java:3687)
08-19 01:15:00.726: E/AndroidRuntime(12657):    at java.lang.reflect.Method.invokeNative(Native Method)
08-19 01:15:00.726: E/AndroidRuntime(12657):    at java.lang.reflect.Method.invoke(Method.java:507)
08-19 01:15:00.726: E/AndroidRuntime(12657):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-19 01:15:00.726: E/AndroidRuntime(12657):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-19 01:15:00.726: E/AndroidRuntime(12657):    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

2

据我了解,您正在尝试将数据从一项活动发送到另一项活动,以便您可以删除与数据匹配的记录?

你有联系 pm = null; 那么您正试图获取数据库中空记录的 id。我确定你关注这个 http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/ 删除

Contact pm = null;
pm._id= i.getExtras().getInt("id");
pm._name = i.getExtras().getString("name");
pm._phone_number = i.getExtras().getString("phone_number");

添加

Contact pm = db.getContact(i.getExtras().getInt("id"));

这将从您在意图中发送的内容中删除具有相同 ID 的记录

于 2013-08-18T20:06:31.597 回答