0

我在這裏有一個小問題,我創建的應用程序,使得用戶將自己的聯繫人的詳細信息,並且可以通過按下再次打電話給他們他們從ListView和thier數據應出現在新的活動來編輯或者刪除應用落在當我選擇從一個SQLite列表中的用戶和ID傳遞到另一個活動

的問題是,當我選擇一個聯繫人應用程序崩潰....在哪裏我做錯?

的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 = [email protected] 
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 

回答

1
SQLiteException: near "=": syntax error (code 1): , while compiling: 
select * from contactsdisplayWHERE id = -1 

你需要的表名和WHERE之間的空間:

select * from contactsdisplay WHERE id = -1 
+0

確定THX,但現在我得到下面 –

+0

01-08 17:11:44.986:E/AndroidRuntime(20823):在編譯,:android.database.sqlite.SQLiteException:沒有這樣的柱:ID(碼1)致:從contactsdisplay SELECT * WHERE ID = -1 –

+0

這data.put( 「_ ID」,find.getString(0))說,你的列名是_ID,列ID不存在。 – Aiapaec

相關問題