每当我尝试添加新联系人或更新现有联系人时,我的应用似乎停止了
我的应用很简单,我让用户输入他的联系人的数据(名字,姓氏,手机......等),然后将其保存到数据库,然后他就可以在主列表视图中看到它(还没有: ))然后他可以选择要显示哪个联系人以编辑或删除他
你可以帮我解决那些崩溃问题吗?下面是数据库类
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类: public class CreateContact extends Activity实现OnClickListener { 按钮创建; TextView fname,lname,mobile,address; 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;
}
}
}
logcat
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
答案 0 :(得分:2)
我可以在代码中看到的一个问题是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
}
答案 1 :(得分:2)
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
并删除此处的强制转换:
public SQLiteDatabase(OnClickListener onClickListener) {
super((Context) onClickListener,
重命名数据库类也是个好主意,因为Android已经有了一个同名的类。
更新:现在您有SQL语法错误:
Create tablecontactsdisplay(_idinteger primary key autoincrement,first nameTEXTlast nameTEXTmobileTEXTaddressTEXT);
您需要在标识符和关键字之间使用空格,并且标识符需要删除其空格或引用整个标识符。此外,还有,
个逗号。 E.g。
Create table contactsdisplay(_id integer primary key autoincrement,`first name` TEXT, `last name` TEXT, mobile TEXT, address TEXT);
答案 2 :(得分:0)
你的问题似乎在这一行:
SQLiteDatabase addition = new SQLiteDatabase(this);
在此上下文中,this
指的是onClickListener的实例,它不是上下文的实例
如果您需要参考活动实例,则应使用CreateContact.this
答案 3 :(得分:0)
实施界面OnClickListener
,而不是将其用作“内部”,然后在那里完成工作。
而不是:
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)";
你忘了用逗号分隔每个键。
答案 4 :(得分:0)
所以我改变了这个
@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的建议改变了一些东西:)
thx guys