[英]my contacts app stops when i try to write or update to my SQLite database
每當我嘗試添加新聯系人或更新現有聯系人時,我的應用似乎都會停止
我的應用程序很簡單,我讓用戶輸入聯系人的數據(名字,姓氏,移動電話等),然后將其保存到數據庫中,然后就可以在主列表視圖中看到它了(尚未完成:)),然后然后他可以選擇要顯示的聯系人以對其進行編輯或刪除
你能幫我解決那些崩潰嗎?
下面是數據庫類
public class DBcreator extends SQLiteOpenHelper {
/*
* this class is responsible for all the database manipulation and
* requirements it is called inside other classes for implementation
*/
public static final String DATABASE_NAME = "contacts";// NAME OF THE
// DATABASE//
public static final String DATABASE_TABLE = "contactsdisplay";// NAME OF THE
// TABLE
// INSIDE OF
// IT//
// THE DATA INSIDE THE TABLE//
public static final String KEY_FNAME = "first name";
public static final String KEY_LNAME = "last name";
public static final String KEY_MOBILE = "mobile";
public static final String KEY_ADDRESS = "address";
public static final String KEY_ROWID = "_id";
// VERSION OF THE DATABASE//
public static final int DATABASE_VERSION = 1;
// create database//
public static final String DATABASE_CREATE = "Create table"
+ DATABASE_TABLE + "(" + KEY_ROWID
+ "integer primary key autoincrement," + KEY_FNAME + "TEXT"
+ KEY_LNAME + "TEXT" + KEY_MOBILE + "TEXT" + KEY_ADDRESS + "TEXT);";
public DBcreator(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(android.database.sqlite.SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(DATABASE_CREATE);// this orders the code to create the
// database//
}
@Override
public void onUpgrade(android.database.sqlite.SQLiteDatabase db,
int oldVersion, int newVersion) {
// drop old version and use the new one//
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
public long add(String fname, String lname, String mob, String address) {
// getting the database and editing in it//
SQLiteDatabase db = this.getWritableDatabase();
// insert what the user had entered in the database//
ContentValues cv = new ContentValues();
cv.put(KEY_FNAME, fname);
cv.put(KEY_LNAME, lname);
cv.put(KEY_MOBILE, mob);
cv.put(KEY_ADDRESS, address);
// db.insert(DATABASE_TABLE, null, cv);
return db.insert(DATABASE_TABLE, null, cv);
}
public void savechanges(String fname, String lname, String mob,
String address) {
// change the existing data of the contact by lookup then replace//
SQLiteDatabase db = this.getWritableDatabase();
String[] array = new String[] { KEY_ROWID, KEY_FNAME, KEY_LNAME,
KEY_MOBILE, KEY_ADDRESS };
Cursor c = db.query(DATABASE_TABLE, array, fname, null, null, null,
null);
// we need to loop inside the database to find the existing values and
// replace them//
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
int firstname = c.getColumnIndex(KEY_FNAME);
int lastname = c.getColumnIndex(KEY_FNAME);
int mobile = c.getColumnIndex(KEY_FNAME);
int address2 = c.getColumnIndex(KEY_FNAME);
if (c.getString(firstname).equals(fname)) {
ContentValues changes = new ContentValues();
changes.put(KEY_FNAME, fname);
changes.put(KEY_LNAME, lname);
changes.put(KEY_MOBILE, mob);
changes.put(KEY_ADDRESS, address);
db.update(DATABASE_TABLE, changes, KEY_FNAME + "=" + fname,
null);
break;
}
}
}
public void deletecontact(String fnamedelete) {
// TODO Auto-generated method stub
SQLiteDatabase db = this.getWritableDatabase();
db.delete(DATABASE_TABLE, KEY_FNAME + "=" + fnamedelete, null);
}
public String[] queryall() {
String[] FirstNames = new String[] { KEY_FNAME };
SQLiteDatabase db = this.getWritableDatabase();
Cursor cfname = db.query(DATABASE_TABLE, FirstNames, null, null, null,
null, null);// to see all contacts//
int GetTheName = cfname.getColumnIndex(KEY_FNAME);
List<String> TheNames = new ArrayList<String>();
{
cfname.moveToFirst();
while (cfname.moveToNext()) {
TheNames.add(cfname.getString(GetTheName));
}
return TheNames.toArray(new String[TheNames.size()]);
}
}
}
下面是CreateContact類:公共類CreateContact擴展Activity實現OnClickListener {Button Create; TextView的fname,lname,mobile,地址; EditText editfirst,editlast,editmobile,editaddress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.createcontact);
Create = (Button) findViewById(R.id.SaveNewContact);
fname = (TextView) findViewById(R.id.FirstNametv1);
lname = (TextView) findViewById(R.id.LastNametv1);
mobile = (TextView) findViewById(R.id.Mobiletv1);
address = (TextView) findViewById(R.id.Addresstv1);
editfirst = (EditText) findViewById(R.id.EditFirstName);
editlast = (EditText) findViewById(R.id.EditLastName);
editmobile = (EditText) findViewById(R.id.EditMobile);
editaddress = (EditText) findViewById(R.id.EditAddress);
Create.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DBcreator addition = new DBcreator(CreateContact.this);
// her you ADD (put) the new data to the SQLite//
String fname = editfirst.getEditableText().toString();
String lname = editlast.getEditableText().toString();
String mob = editmobile.getEditableText().toString();
String address = editaddress.getEditableText().toString();
addition.add(fname, lname, mob, address);
addition.close();
// go back to the main activity after changes took place//
Intent k = new Intent(CreateContact.this, MainActivity.class);
startActivity(k);
}
}
下面是EditContact類:
public class EditContact extends Activity implements OnClickListener {
Button SaveEdit, Cancel, Delete;
TextView fname2, lname2, mobile2, address2;
EditText editfirst2, editlast2, editmobile2, editaddress2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editcontact);
SaveEdit = (Button) findViewById(R.id.SaveChanges);
Cancel = (Button) findViewById(R.id.CancelButton);
Delete = (Button) findViewById(R.id.DeleteContact);
fname2 = (TextView) findViewById(R.id.FirstNametv2);
lname2 = (TextView) findViewById(R.id.LastNametv2);
mobile2 = (TextView) findViewById(R.id.Mobiletv2);
address2 = (TextView) findViewById(R.id.Addresstv2);
editfirst2 = (EditText) findViewById(R.id.ViewFirstName);
editlast2 = (EditText) findViewById(R.id.ViewLastName);
editmobile2 = (EditText) findViewById(R.id.ViewMobile);
editaddress2 = (EditText) findViewById(R.id.ViewAddress);
SaveEdit.setOnClickListener(this);
Cancel.setOnClickListener(this);
Delete.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.SaveChanges:
DBcreator edit = new DBcreator(EditContact.this);
String fname = editfirst2.getEditableText().toString();
String lname = editlast2.getEditableText().toString();
String mob = editmobile2.getEditableText().toString();
String address = editaddress2.getEditableText().toString();
edit.savechanges(fname, lname, mob, address);
edit.close();
Intent y = new Intent(EditContact.this, MainActivity.class);
startActivity(y);
break;
case R.id.CancelButton:
Intent k = new Intent(EditContact.this, MainActivity.class);
startActivity(k);
break;
case R.id.DeleteContact:
DBcreator delete = new DBcreator(EditContact.this);
String fnamedelete = editfirst2.getEditableText().toString();
String lnamedelete = editlast2.getEditableText().toString();
String mobdelete = editmobile2.getEditableText().toString();
String addressdelete = editaddress2.getEditableText().toString();
delete.deletecontact(fnamedelete);
delete.close();
Intent h = new Intent(EditContact.this, MainActivity.class);
startActivity(h);
break;
}
}
}
日志貓
12-30 12:08:31.794: E/Trace(11691): error opening trace file: No such file or directory (2)
12-30 12:08:32.184: W/MMUMapper(11691): fail to register MVA, unsupported format(0x5)
12-30 12:08:32.477: W/MMUMapper(11691): fail to register MVA, unsupported format(0x5)
12-30 12:08:32.530: W/MMUMapper(11691): fail to register MVA, unsupported format(0x5)
12-30 12:08:33.303: I/SurfaceTextureClient(11691): [0x4f05e390] frames:9, duration:1.000000, fps:8.996834
12-30 12:08:34.680: W/MMUMapper(11691): fail to register MVA, unsupported format(0x5)
12-30 12:08:34.719: I/SurfaceTextureClient(11691): [0x4f05e390] frames:11, duration:1.407000, fps:7.816139
12-30 12:08:34.958: W/MMUMapper(11691): fail to register MVA, unsupported format(0x5)
12-30 12:08:35.019: W/MMUMapper(11691): invalid operation for unregister MVA with VA(0x526d0000) size(614400) f(0x5)
12-30 12:08:35.020: W/MMUMapper(11691): invalid operation for unregister MVA with VA(0x5295d000) size(614400) f(0x5)
12-30 12:08:35.020: W/MMUMapper(11691): invalid operation for unregister MVA with VA(0x52b00000) size(614400) f(0x5)
12-30 12:08:35.270: W/MMUMapper(11691): fail to register MVA, unsupported format(0x5)
12-30 12:08:36.274: I/SurfaceTextureClient(11691): [0x529225b8] frames:4, duration:1.462000, fps:2.734245
12-30 12:08:37.780: I/SurfaceTextureClient(11691): [0x529225b8] frames:4, duration:1.495000, fps:2.674925
12-30 12:08:38.882: I/SurfaceTextureClient(11691): [0x529225b8] frames:3, duration:1.099000, fps:2.729083
12-30 12:08:40.204: I/SurfaceTextureClient(11691): [0x529225b8] frames:4, duration:1.259000, fps:3.176750
12-30 12:08:41.403: I/SurfaceTextureClient(11691): [0x529225b8] frames:4, duration:1.267000, fps:3.156914
12-30 12:08:42.486: I/SurfaceTextureClient(11691): [0x529225b8] frames:4, duration:1.072000, fps:3.729460
12-30 12:08:43.690: I/SurfaceTextureClient(11691): [0x529225b8] frames:5, duration:1.215000, fps:4.115116
12-30 12:08:44.736: I/SurfaceTextureClient(11691): [0x529225b8] frames:4, duration:1.046000, fps:3.823227
12-30 12:08:46.119: I/SurfaceTextureClient(11691): [0x529225b8] frames:5, duration:1.379000, fps:3.625341
12-30 12:08:47.287: I/SurfaceTextureClient(11691): [0x529225b8] frames:4, duration:1.176000, fps:3.399773
12-30 12:08:48.622: I/SurfaceTextureClient(11691): [0x529225b8] frames:4, duration:1.324000, fps:3.019432
12-30 12:08:49.734: I/SurfaceTextureClient(11691): [0x529225b8] frames:5, duration:1.119000, fps:4.465023
12-30 12:08:50.987: I/SurfaceTextureClient(11691): [0x529225b8] frames:4, duration:1.155000, fps:3.463197
12-30 12:08:52.023: I/SurfaceTextureClient(11691): [0x529225b8] frames:3, duration:1.140000, fps:2.629971
12-30 12:08:52.226: E/SQLiteLog(11691): (1) near "tablecontactsdisplay": syntax error
12-30 12:08:52.227: W/dalvikvm(11691): threadid=1: thread exiting with uncaught exception (group=0x416e4908)
12-30 12:08:52.255: E/AndroidRuntime(11691): FATAL EXCEPTION: main
12-30 12:08:52.255: E/AndroidRuntime(11691): android.database.sqlite.SQLiteException: near "tablecontactsdisplay": syntax error (code 1): , while compiling: Create tablecontactsdisplay(_idinteger primary key autoincrement,first nameTEXTlast nameTEXTmobileTEXTaddressTEXT);
12-30 12:08:52.255: E/AndroidRuntime(11691): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
12-30 12:08:52.255: E/AndroidRuntime(11691): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
12-30 12:08:52.255: E/AndroidRuntime(11691): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
12-30 12:08:52.255: E/AndroidRuntime(11691): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
12-30 12:08:52.255: E/AndroidRuntime(11691): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
12-30 12:08:52.255: E/AndroidRuntime(11691): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
12-30 12:08:52.255: E/AndroidRuntime(11691): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
12-30 12:08:52.255: E/AndroidRuntime(11691): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
12-30 12:08:52.255: E/AndroidRuntime(11691): at com.hossa.contactsapp.DBcreator.onCreate(DBcreator.java:45)
12-30 12:08:52.255: E/AndroidRuntime(11691): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
12-30 12:08:52.255: E/AndroidRuntime(11691): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
12-30 12:08:52.255: E/AndroidRuntime(11691): at com.hossa.contactsapp.DBcreator.add(DBcreator.java:60)
12-30 12:08:52.255: E/AndroidRuntime(11691): at com.hossa.contactsapp.CreateContact.onClick(CreateContact.java:50)
12-30 12:08:52.255: E/AndroidRuntime(11691): at android.view.View.performClick(View.java:4093)
12-30 12:08:52.255: E/AndroidRuntime(11691): at android.view.View$PerformClick.run(View.java:17149)
12-30 12:08:52.255: E/AndroidRuntime(11691): at android.os.Handler.handleCallback(Handler.java:615)
12-30 12:08:52.255: E/AndroidRuntime(11691): at android.os.Handler.dispatchMessage(Handler.java:92)
12-30 12:08:52.255: E/AndroidRuntime(11691): at android.os.Looper.loop(Looper.java:153)
12-30 12:08:52.255: E/AndroidRuntime(11691): at android.app.ActivityThread.main(ActivityThread.java:5006)
12-30 12:08:52.255: E/AndroidRuntime(11691): at java.lang.reflect.Method.invokeNative(Native Method)
12-30 12:08:52.255: E/AndroidRuntime(11691): at java.lang.reflect.Method.invoke(Method.java:511)
12-30 12:08:52.255: E/AndroidRuntime(11691): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
12-30 12:08:52.255: E/AndroidRuntime(11691): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
12-30 12:08:52.255: E/AndroidRuntime(11691): at dalvik.system.NativeStart.main(Native Method)
12-30 12:08:54.208: I/Process(11691): Sending signal. PID: 11691 SIG: 9
在代碼中可以看到的一個問題是DATABASE_CREATE字符串。 Neead在單詞之間添加了一些空格和逗號:
public static final String DATABASE_CREATE = "Create table "
+ DATABASE_TABLE + "(" + KEY_ROWID
+ " integer primary key autoincrement," + KEY_FNAME + " TEXT,"
+ KEY_LNAME + " TEXT," + KEY_MOBILE + " TEXT," + KEY_ADDRESS + " TEXT);";
另一個問題是SQLiteDatabase的構造函數,未正確聲明
public SQLiteDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
12-29 14:02:33.748: E/AndroidRuntime(31505): java.lang.ClassCastException: com.hossa.contactsapp.CreateContact$1 cannot be cast to android.content.Context
12-29 14:02:33.748: E/AndroidRuntime(31505): at com.hossa.contactsapp.SQLiteDatabase.<init>(SQLiteDatabase.java:35)
匿名內部類CreateContact$1
不是Context
。
您不應該盲目地信任IDE的問題解決能力:
SQLiteDatabase addition = new SQLiteDatabase(this);
在這里,您應該傳遞一個Context
。 在Activity
,可以通過this
但在OnClickListener
,它是指列表器,而不是活動。 使用CreateContact.this
指父this
是一個Context
。
然后,將傳遞給SQLiteDatabase
構造函數的參數更改為Context
並在此處刪除SQLiteDatabase
轉換:
public SQLiteDatabase(OnClickListener onClickListener) {
super((Context) onClickListener,
重命名數據庫類也是一個好主意,因為Android已經具有相同名稱的類。
更新:現在您在這里有SQL語法錯誤:
Create tablecontactsdisplay(_idinteger primary key autoincrement,first nameTEXTlast nameTEXTmobileTEXTaddressTEXT);
您需要在標識符和關鍵字之間使用空格,並且標識符需要刪除其空格或用引號將整個標識符引起來。 也有缺失,
逗號。 例如
Create table contactsdisplay(_id integer primary key autoincrement,`first name` TEXT, `last name` TEXT, mobile TEXT, address TEXT);
您的問題似乎在這一行: SQLiteDatabase addition = new SQLiteDatabase(this);
在這種情況下, this
是指onClickListener的實例,如果您需要引用活動的實例,則不是上下文的實例,應使用CreateContact.this
實現接口OnClickListener
而不是將其用作“ inner ”,然后在那里進行工作。
代替這個:
Create.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SQLiteDatabase addition = new SQLiteDatabase(CreateContact.this);
// her you ADD (put) the new data to the SQLite//
String fname = editfirst.getEditableText().toString();
String lname = editlast.getEditableText().toString();
String mob = editmobile.getEditableText().toString();
String address = editaddress.getEditableText().toString();
addition.add(fname, lname, mob, address);
addition.close();
// go back to the main activity after changes took place//
Intent k = new Intent(CreateContact.this, MainActivity.class);
startActivity(k);
}
});
這樣做: Create.setOnClickListener(this);
public class CreateContact extends Activity implements View.OnClickListener {
//etc... etc..
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SQLiteDatabase addition = new SQLiteDatabase(this);
// her you ADD (put) the new data to the SQLite//
String fname = editfirst.getEditableText().toString();
String lname = editlast.getEditableText().toString();
String mob = editmobile.getEditableText().toString();
String address = editaddress.getEditableText().toString();
addition.add(fname, lname, mob, address);
addition.close();
// go back to the main activity after changes took place//
Intent k = new Intent(CreateContact.this, MainActivity.class);
startActivity(k);
}
}
編輯
您的數據庫架構錯誤,logcat表示存在語法錯誤,它應如下所示:
private static final String DATABASE_CREATE =
"CREATE TABLE "
+ DATABASE_TABLE + "( "
+ KEY_ROWID + " INTEGER PRIMARY KEY,"
+ KEY_FNAME + " TEXT,"
+ KEY_LNAME + " TEXT,"
+ KEY_MOBILE + " TEXT,"
+ KEY_ADDRESS + " TEXT)";
您忘了用逗號分隔每個鍵。
謝謝幫手,我剛剛發現這些類在實現我刪除的另一個類
所以我改變了這個
@Override
public void onCreate(android.database.sqlite.SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(DATABASE_CREATE);// this orders the code to create the
// database//
}
對此
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(DATABASE_CREATE);// this orders the code to create the
// database//
}
您會看到我的舊類稱為SQLitedatabase,因此我遵循了@laalto和@ramaral建議並更改了一些內容:)
謝謝你們
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.