在我的旧模拟器中,该程序可以轻松运行,没有任何问题。所以我认为程序很好。但是当我用新的模拟器/或其他模拟器尝试它时,它也可以运行。当我单击搜索按钮时,问题就开始了。当我单击时,程序意外关闭。Logcat 说非法状态异常并由以下原因引起:没有这样的列 as_id。但在我的数据库中存在 _id 列。我可以毫无例外地在我的旧模拟器中搜索。相同的代码相同的数据库。为什么会发生这种情况?为什么它只在一个中运行模拟器?任何人请告诉如何解决异常。
这是我的程序中断的java类
`
public class EmployeeList extends ListActivity {
protected EditText searchText;
protected SQLiteDatabase db;
protected Cursor cursor;
protected ListAdapter adapter;
public Integer pid=null;
public DatabaseHelper databaseHelper;
public Book book;
private static final int DELETE_ID = Menu.FIRST+3;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = (new DatabaseHelper(this)).getWritableDatabase();
searchText = (EditText) findViewById (R.id.searchText);
}
public void search(View view) { //when i click here i get exception
// || is the concatenation operation in SQLite
Toast.makeText(getApplicationContext(), "insert successfull", Toast.LENGTH_LONG).show();
cursor = db.rawQuery("SELECT _id, firstName, lastName, title FROM employee WHERE firstName || ' ' || lastName LIKE ?",
new String[]{"%" + searchText.getText().toString() + "%"});
adapter = new SimpleCursorAdapter(
this,
R.layout.employee_list_item,
cursor,
new String[] {"firstName", "lastName", "title"},
new int[] {R.id.firstName, R.id.lastName, R.id.title});
setListAdapter(adapter);
registerForContextMenu(getListView());
}
public void onListItemClick(ListView parent, View view, int position, long id) {
Intent intent = new Intent(this, EmployeeDetails.class);
Cursor cursor = (Cursor) adapter.getItem(position);
intent.putExtra("EMPLOYEE_ID", cursor.getInt(cursor.getColumnIndex("_id")));
startActivity(intent);
}
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
menu.add(Menu.NONE, DELETE_ID, Menu.NONE, "Delete")
.setAlphabeticShortcut('d');
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case DELETE_ID:
AdapterView.AdapterContextMenuInfo info=
(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
delete(info.id);
return(true);
}
return(super.onOptionsItemSelected(item));
}
private void delete(final long rowId) {
if (rowId>0) {
new AlertDialog.Builder(this)
.setTitle(R.string.delete_title)
.setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
processDelete(rowId);
}
})
.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// ignore, just dismiss
}
})
.show();
}
}
private void processDelete(long rowId) {
String[] args={String.valueOf(rowId)};
databaseHelper.getWritableDatabase().delete("employee", "_ID=?", args);
cursor.requery();
}
} `
我的数据库类:
`public class DatabaseHelper extends SQLiteOpenHelper{
public static final String DB_NAME = "employee_directory";
public static final Integer VERSION=1;
public static final String TABLE_NAME= "employee";
public static final String _id= "id";
public static final String firstName= "firstName";
public static final String lastName= "lastName";
public static final String title= "title";
public static final String officePhone= "officePhone";
public static final String cellPhone= "cellPhone";
public static final String email= "email";
public static final String TABLE_SQL = "CREATE TABLE " + TABLE_NAME+" (" +_id+" INTEGER PRIMARY KEY AUTOINCREMENT, "
+ firstName+ " TEXT, " + lastName+ " TEXT, " + title+ " TEXT, " + officePhone+ " TEXT, " + cellPhone+" TEXT, " + email+" TEXT)";
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
Log.d("TABLE SQL", TABLE_SQL);
db.execSQL(TABLE_SQL);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
db.execSQL("DROP TABLE IF EXISTS employees");
onCreate(db);
}
}`
我的日志猫说:
` 08-10 01:23:12.655: E/AndroidRuntime(337): FATAL EXCEPTION: main
08-10 01:23:12.655: E/AndroidRuntime(337): java.lang.IllegalStateException: Could not execute method of the activity
08-10 01:23:12.655: E/AndroidRuntime(337): at android.view.View$1.onClick(View.java:2072)
08-10 01:23:12.655: E/AndroidRuntime(337): at android.view.View.performClick(View.java:2408)
08-10 01:23:12.655: E/AndroidRuntime(337): at android.view.View$PerformClick.run(View.java:8816)
08-10 01:23:12.655: E/AndroidRuntime(337): at android.os.Handler.handleCallback(Handler.java:587)
08-10 01:23:12.655: E/AndroidRuntime(337): at android.os.Handler.dispatchMessage(Handler.java:92)
08-10 01:23:12.655: E/AndroidRuntime(337): at android.os.Looper.loop(Looper.java:123)
08-10 01:23:12.655: E/AndroidRuntime(337): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-10 01:23:12.655: E/AndroidRuntime(337): at java.lang.reflect.Method.invokeNative(Native Method)
08-10 01:23:12.655: E/AndroidRuntime(337): at java.lang.reflect.Method.invoke(Method.java:521)
08-10 01:23:12.655: E/AndroidRuntime(337): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-10 01:23:12.655: E/AndroidRuntime(337): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-10 01:23:12.655: E/AndroidRuntime(337): at dalvik.system.NativeStart.main(Native Method)
08-10 01:23:12.655: E/AndroidRuntime(337): Caused by: java.lang.reflect.InvocationTargetException
08-10 01:23:12.655: E/AndroidRuntime(337): at com.example.again.EmployeeList.search(EmployeeList.java:46)
08-10 01:23:12.655: E/AndroidRuntime(337): at java.lang.reflect.Method.invokeNative(Native Method)
08-10 01:23:12.655: E/AndroidRuntime(337): at java.lang.reflect.Method.invoke(Method.java:521)
08-10 01:23:12.655: E/AndroidRuntime(337): at android.view.View$1.onClick(View.java:2067)
08-10 01:23:12.655: E/AndroidRuntime(337): ... 11 more
08-10 01:23:12.655: E/AndroidRuntime(337): Caused by: android.database.sqlite.SQLiteException: no such column: _id: , while compiling: SELECT _id, firstName, lastName, title FROM employee WHERE firstName || ' ' || lastName LIKE ?
08-10 01:23:12.655: E/AndroidRuntime(337): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
08-10 01:23:12.655: E/AndroidRuntime(337): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
08-10 01:23:12.655: E/AndroidRuntime(337): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
08-10 01:23:12.655: E/AndroidRuntime(337): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
08-10 01:23:12.655: E/AndroidRuntime(337): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
08-10 01:23:12.655: E/AndroidRuntime(337): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
08-10 01:23:12.655: E/AndroidRuntime(337): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
08-10 01:23:12.655: E/AndroidRuntime(337): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1315)
08-10 01:23:12.655: E/AndroidRuntime(337): ... 15 more `