我正在开发一个 android 应用程序,我必须在其中存储联系人信息。该应用程序存储联系人并正确检索它们,除非并且直到我重新启动应用程序。重新启动应用程序后,当我尝试检索值时,程序意外崩溃。如果我重新输入值或插入新值,然后尝试检索值,则会检索所有联系人,包括以前的联系人。
任何人都可以解释这个问题的原因和设备解决方案吗?提前致谢。
这是联系人类:
public class Contact {
//private variables
int _id;
String _name;
String _phone_number;
// Empty constructor
public Contact(){
}
// constructor
public Contact(int id, String name, String _phone_number){
this._id = id;
this._name = name;
this._phone_number = _phone_number;
}
// constructor
public Contact(String name, String _phone_number){
this._name = name;
this._phone_number = _phone_number;
}
// getting ID
public int getID(){
return this._id;
}
// setting id
public void setID(int id){
this._id = id;
}
// getting name
public String getName(){
return this._name;
}
// setting name
public void setName(String name){
this._name = name;
}
// getting phone number
public String getPhoneNumber(){
return this._phone_number;
}
// setting phone number
public void setPhoneNumber(String phone_number){
this._phone_number = phone_number;
}
}
这是数据库处理程序类:
public class DatabaseHandler extends SQLiteOpenHelper{
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "contactsManager";
// Contacts table name
private static final String TABLE_CONTACTS = "contacts";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_PH_NO = "phone_number";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_PH_NO + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
// Create tables again
onCreate(db);
}
public void addContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName()); // Contact Name
values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone Number
// Inserting Row
db.insert(TABLE_CONTACTS, null, values);
db.close(); // Closing database connection
}
public void delete() {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("delete * from "+ TABLE_CONTACTS);
db.close(); // Closing database connection
}
// Getting All Contacts
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setPhoneNumber(cursor.getString(2));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
}
这是LogCat:
01-13 18:46:36.734: D/dalvikvm(1166): GC_EXTERNAL_ALLOC freed 70K, 50% free 2692K/5379K, external 1859K/2108K, paused 20ms
01-13 18:46:38.687: D/AndroidRuntime(1166): Shutting down VM
01-13 18:46:38.687: W/dalvikvm(1166): threadid=1: thread exiting with uncaught exception (group=0x40015578)
01-13 18:46:38.687: E/AndroidRuntime(1166): FATAL EXCEPTION: main
01-13 18:46:38.687: E/AndroidRuntime(1166): java.lang.NullPointerException
01-13 18:46:38.687: E/AndroidRuntime(1166): at com.example.tourguide_final.TourGuide$1.onClick(TourGuide.java:75)
01-13 18:46:38.687: E/AndroidRuntime(1166): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159)
01-13 18:46:38.687: E/AndroidRuntime(1166): at android.os.Handler.dispatchMessage(Handler.java:99)
01-13 18:46:38.687: E/AndroidRuntime(1166): at android.os.Looper.loop(Looper.java:123)
01-13 18:46:38.687: E/AndroidRuntime(1166): at android.app.ActivityThread.main(ActivityThread.java:3687)
01-13 18:46:38.687: E/AndroidRuntime(1166): at java.lang.reflect.Method.invokeNative(Native Method)
01-13 18:46:38.687: E/AndroidRuntime(1166): at java.lang.reflect.Method.invoke(Method.java:507)
01-13 18:46:38.687: E/AndroidRuntime(1166): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
01-13 18:46:38.687: E/AndroidRuntime(1166): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-13 18:46:38.687: E/AndroidRuntime(1166): at dalvik.system.NativeStart.main(Native Method)