我正在开发一个Android应用程序,我必须存储联系人信息。应用程序存储联系人并正确检索它们,除非我重新启动应用程序。重新启动应用程序后,当我尝试检索值时,程序意外崩溃。如果我重新输入值或插入新值然后尝试检索值,则会检索所有联系人,包括之前的联系人。
有人可以解释原因和设备解决这个问题的方法吗? 提前谢谢。
以下是Contacts类:
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)
答案 0 :(得分:0)
请浏览此链接以处理运行时更改,这可能对您有所帮助 http://developer.android.com/guide/topics/resources/runtime-changes.html
答案 1 :(得分:0)
您在TourGuide $ 1.onClick第75行上有一个NPE。如果您使用的是Eclipse,只需在日志中双击此行,它就会转到问题行。从视觉检查中,您可以找出哪些变量尚未正确初始化。
答案 2 :(得分:0)
1.) Remove void delete() method .What it is doing at the end of the program it is deleting data from the database .Whwn you restart your program ,coompiler doesn't get any data and throws exception
2.)OR What you can do after restarting 1st call void delete method then insert method and then at last call select query method