我在这里遇到一个小问题,我创建的应用程序会让用户添加自己的联系人详细信息,并可以通过从列表视图中按下它们再次调用它们,并且它们应该出现在新活动上以编辑或删除它们< / p>
问题是当我选择联系人时应用程序崩溃....我哪里出错?
MainActivity.java:
包com.hossa.contactsapp;
public class MainActivity extends Activity {
Button ADD;
ListView LS;
DBcreator dbview;
SQLiteDatabase db;
ArrayList<HashMap<String, String>> List = new ArrayList<HashMap<String, String>>();// define
// an
// arraylist
// with
// hashmap
// for
// ref.//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ADD = (Button) findViewById(R.id.AddButton);
LS = (ListView) findViewById(R.id.ContactsList);
/*
* SimpleAdapter sa = new SimpleAdapter(this, List,
* R.layout.activity_main, new String[] { DBcreator.KEY_ROWID,
* DBcreator.KEY_FNAME, DBcreator.KEY_LNAME, DBcreator.KEY_MOBILE,
* DBcreator.KEY_ADDRESS }, new int[] { R.id.ContactsList });
*
* LS.setAdapter(sa);
*/
existingcontacts();
LS.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// what happens when you select a contact
Intent j = new Intent(v.getContext(), EditContact.class);
j.putExtra("ID", id);
startActivity(j);
}
});
ADD.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent j = new Intent(MainActivity.this, CreateContact.class);
startActivity(j);
}
});
}
private void existingcontacts() {
// TODO Auto-generated method stub
DBcreator db = new DBcreator(MainActivity.this);
SQLiteDatabase database = db.getReadableDatabase();
Cursor find = database.rawQuery("select * from contactsdisplay", null);// go
// read
// from
// the
// database
// contactsdisplay//
// below is the adapter to put the intended content in the list view//
SimpleCursorAdapter cursor = new SimpleCursorAdapter(MainActivity.this,
R.layout.listviewdistribution, find, new String[] {
DBcreator.KEY_FNAME, DBcreator.KEY_LNAME,
DBcreator.KEY_MOBILE, DBcreator.KEY_ADDRESS },
new int[] { R.id.FirstNameTextView, R.id.LastNameTextView,
R.id.MobileTextView, R.id.AddressTextView });
// below is looping with the cursor inside the table to get all data//
while (find.moveToNext()) {
HashMap<String, String> data = new HashMap<String, String>();
data.put("_ID", find.getString(0));
data.put("FirstName", find.getString(1));
data.put("LastName", find.getString(2));
data.put("Mobile", find.getString(3));
data.put("Address", find.getString(4));
List.add(data);
}
LS.setAdapter(cursor);
}
@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;
}
}
EditContact.java:
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);
Intent i = new Intent();
final int getid = i.getIntExtra("ID", -1);
DBcreator db = new DBcreator(this);
SQLiteDatabase getdata = db.getWritableDatabase();
Cursor c = getdata.rawQuery("select * from " + DBcreator.DATABASE_TABLE
+ "WHERE id = " + getid, null);
if (c != null) {
if (c.moveToFirst()) {
do {
editfirst2.setText(c.getString(0));
editlast2.setText(c.getString(1));
editmobile2.setText(c.getString(3));
editaddress2.setText(c.getString(4));
} while (c.moveToNext());
}
}
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.open();
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.open();
delete.deletecontact(fnamedelete);
delete.close();
Intent h = new Intent(EditContact.this, MainActivity.class);
startActivity(h);
break;
}
}
}
LOGCAT:
01-08 16:57:24.718: E/Trace(19947): error opening trace file: No such file or directory (2)
01-08 16:57:25.121: W/MMUMapper(19947): fail to register MVA, unsupported format(0x5)
01-08 16:57:25.633: W/MMUMapper(19947): fail to register MVA, unsupported format(0x5)
01-08 16:57:25.802: W/MMUMapper(19947): fail to register MVA, unsupported format(0x5)
01-08 16:57:26.272: I/SurfaceTextureClient(19947): [0x51770780] frames:12, duration:1.011000, fps:11.866401
01-08 16:57:29.258: W/MMUMapper(19947): invalid operation for unregister MVA with VA(0x528b8000) size(614400) f(0x5)
01-08 16:57:29.259: W/MMUMapper(19947): invalid operation for unregister MVA with VA(0x52e65000) size(614400) f(0x5)
01-08 16:57:29.260: W/MMUMapper(19947): invalid operation for unregister MVA with VA(0x52b5f000) size(614400) f(0x5)
01-08 16:57:29.559: W/MMUMapper(19947): fail to register MVA, unsupported format(0x5)
01-08 16:57:29.757: W/MMUMapper(19947): fail to register MVA, unsupported format(0x5)
01-08 16:57:29.963: W/MMUMapper(19947): fail to register MVA, unsupported format(0x5)
01-08 16:57:31.545: W/MMUMapper(19947): invalid operation for unregister MVA with VA(0x52d5f000) size(614400) f(0x5)
01-08 16:57:31.547: W/MMUMapper(19947): invalid operation for unregister MVA with VA(0x52e65000) size(614400) f(0x5)
01-08 16:57:31.555: W/MMUMapper(19947): invalid operation for unregister MVA with VA(0x53011000) size(614400) f(0x5)
01-08 16:57:31.975: W/MMUMapper(19947): fail to register MVA, unsupported format(0x5)
01-08 16:57:32.158: W/MMUMapper(19947): fail to register MVA, unsupported format(0x5)
01-08 16:57:32.654: W/MMUMapper(19947): fail to register MVA, unsupported format(0x5)
01-08 16:57:33.155: I/SurfaceTextureClient(19947): [0x528afd98] frames:5, duration:1.013000, fps:4.931273
01-08 16:57:35.315: I/SurfaceTextureClient(19947): [0x528afd98] frames:10, duration:2.188000, fps:4.570112
01-08 16:57:36.200: I/AbsListView(19947): Fling from scroll with initialVelocity = -160,mActivePointerId = 0,mFirstPosition = 1,firstChildTop = -36,this = android.widget.ListView@415fd8a8
01-08 16:57:36.325: I/SurfaceTextureClient(19947): [0x528afd98] frames:57, duration:1.010000, fps:56.415134
01-08 16:57:37.043: E/SQLiteLog(19947): (1) near "=": syntax error
01-08 16:57:37.043: W/dalvikvm(19947): threadid=1: thread exiting with uncaught exception (group=0x40f4e908)
01-08 16:57:37.063: E/AndroidRuntime(19947): FATAL EXCEPTION: main
01-08 16:57:37.063: E/AndroidRuntime(19947): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hossa.contactsapp/com.hossa.contactsapp.EditContact}: android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while compiling: select * from contactsdisplayWHERE id = -1
01-08 16:57:37.063: E/AndroidRuntime(19947): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2203)
01-08 16:57:37.063: E/AndroidRuntime(19947): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-08 16:57:37.063: E/AndroidRuntime(19947): at android.app.ActivityThread.access$600(ActivityThread.java:150)
01-08 16:57:37.063: E/AndroidRuntime(19947): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1301)
01-08 16:57:37.063: E/AndroidRuntime(19947): at android.os.Handler.dispatchMessage(Handler.java:99)
01-08 16:57:37.063: E/AndroidRuntime(19947): at android.os.Looper.loop(Looper.java:153)
01-08 16:57:37.063: E/AndroidRuntime(19947): at android.app.ActivityThread.main(ActivityThread.java:5006)
01-08 16:57:37.063: E/AndroidRuntime(19947): at java.lang.reflect.Method.invokeNative(Native Method)
01-08 16:57:37.063: E/AndroidRuntime(19947): at java.lang.reflect.Method.invoke(Method.java:511)
01-08 16:57:37.063: E/AndroidRuntime(19947): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
01-08 16:57:37.063: E/AndroidRuntime(19947): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
01-08 16:57:37.063: E/AndroidRuntime(19947): at dalvik.system.NativeStart.main(Native Method)
01-08 16:57:37.063: E/AndroidRuntime(19947): Caused by: android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while compiling: select * from contactsdisplayWHERE id = -1
01-08 16:57:37.063: E/AndroidRuntime(19947): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
01-08 16:57:37.063: E/AndroidRuntime(19947): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
01-08 16:57:37.063: E/AndroidRuntime(19947): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
01-08 16:57:37.063: E/AndroidRuntime(19947): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
01-08 16:57:37.063: E/AndroidRuntime(19947): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
01-08 16:57:37.063: E/AndroidRuntime(19947): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
01-08 16:57:37.063: E/AndroidRuntime(19947): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
01-08 16:57:37.063: E/AndroidRuntime(19947): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
01-08 16:57:37.063: E/AndroidRuntime(19947): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
01-08 16:57:37.063: E/AndroidRuntime(19947): at com.hossa.contactsapp.EditContact.onCreate(EditContact.java:29)
01-08 16:57:37.063: E/AndroidRuntime(19947): at android.app.Activity.performCreate(Activity.java:5076)
01-08 16:57:37.063: E/AndroidRuntime(19947): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
01-08 16:57:37.063: E/AndroidRuntime(19947): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2167)
01-08 16:57:37.063: E/AndroidRuntime(19947): ... 11 more
01-08 16:57:39.693: I/Process(19947): Sending signal. PID: 19947 SIG: 9
答案 0 :(得分:1)
SQLiteException: near "=": syntax error (code 1): , while compiling:
select * from contactsdisplayWHERE id = -1
表名与WHERE
之间需要空格:
select * from contactsdisplay WHERE id = -1