我有一个应用程序应该从sqlit数据库中检索数据。我编写了一个类,其中包含数据库和构造函数,setter和getter函数所需的属性。我还为数据库处理程序编写了类,如下所示:
public DatabaseHandler(Context context , String name,
CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_Books_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_Price + " FLOAT" + ")";
db.execSQL(CREATE_Books_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
onCreate(db);
}
public void addbook(Books book) {
ContentValues values = new ContentValues();
values.put(KEY_NAME, book.getName());
values.put(KEY_Price, book.getprice());
SQLiteDatabase db = this.getWritableDatabase();
db.insert(TABLE_CONTACTS, null, values);
db.close();
}
Books findbook(String name ) {
String query = "Select" + KEY_NAME + "FROM " + TABLE_CONTACTS + " WHERE " +
KEY_NAME + " = \"" + name + "\"";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(query, null);
Books book = new Books();
if (cursor.moveToFirst()){
cursor.moveToFirst();
}
book.setID(Integer.parseInt(cursor.getString(0)));
book.setName(cursor.getString(1));
book.setprice(Float.parseFloat(cursor.getString(2)));
return book;
}
在我刚刚完成的主要活动中:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseHandler db = new DatabaseHandler(this , null, null, 1);
db.addbook(new Books("book1", (float) 5.50));
db.addbook(new Books("book2", (float) 4.30));
db.addbook(new Books("book3", (float) 5.85));
}
public void SearchClickHandler(View view){
DatabaseHandler db = new DatabaseHandler(this , null, null, 1);
if (view.getId() == R.id.Searchbutton) {
EditText BookN = (EditText)findViewById(R.id.bookname);
String Book_n = BookN.getText().toString() ;
TextView resultT = (TextView)findViewById(R.id.resultLable);
Books book = db.findbook(Book_n);
if(book != null ) {
Log.d("Searching for your request ","please wait..");
resultT.setText(Book_n + " is avaliable at our Bookstore, do you " +
"want to buy it ?");
} else {
resultT.setText("We are soory, "+ Book_n + " is not avaliable at our Bookstore");
}
}
}
一切都很好并且没有错误,应用程序在模拟器和设备上运行但是当我点击搜索按钮时 应用程序停止并退出.. 所以,如果有人知道错误在哪里?!
编辑: 这是代码的堆栈跟踪:
02-12 19:03:14.825:I / Choreographer(986):跳过506帧!应用程序可能在其主线程上做了太多工作。 02-12 19:03:16.475:D / AndroidRuntime(986):关闭VM 02-12 19:03:16.475:W / dalvikvm(986):threadid = 1:线程退出,未捕获异常(组= 0x414c4700) 02-12 19:03:16.545:E / AndroidRuntime(986):致命异常:主要 02-12 19:03:16.545:E / AndroidRuntime(986):java.lang.IllegalStateException:无法执行活动的方法 02-12 19:03:16.545:E / AndroidRuntime(986):在android.view.View $ 1.onClick(View.java:3633) 02-12 19:03:16.545:E / AndroidRuntime(986):在android.view.View.performClick(View.java:4240) 02-12 19:03:16.545:E / AndroidRuntime(986):在android.view.View $ PerformClick.run(View.java:17721) 02-12 19:03:16.545:E / AndroidRuntime(986):在android.os.Handler.handleCallback(Handler.java:730) 02-12 19:03:16.545:E / AndroidRuntime(986):在android.os.Handler.dispatchMessage(Handler.java:92) 02-12 19:03:16.545:E / AndroidRuntime(986):在android.os.Looper.loop(Looper.java:137) 02-12 19:03:16.545:E / AndroidRuntime(986):在android.app.ActivityThread.main(ActivityThread.java:5103) 02-12 19:03:16.545:E / AndroidRuntime(986):at java.lang.reflect.Method.invokeNative(Native Method) 02-12 19:03:16.545:E / AndroidRuntime(986):at java.lang.reflect.Method.invoke(Method.java:525) 02-12 19:03:16.545:E / AndroidRuntime(986):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737) 02-12 19:03:16.545:E / AndroidRuntime(986):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 02-12 19:03:16.545:E / AndroidRuntime(986):at dalvik.system.NativeStart.main(Native Method) 02-12 19:03:16.545:E / AndroidRuntime(986):引起:java.lang.reflect.InvocationTargetException 02-12 19:03:16.545:E / AndroidRuntime(986):at java.lang.reflect.Method.invokeNative(Native Method) 02-12 19:03:16.545:E / AndroidRuntime(986):at java.lang.reflect.Method.invoke(Method.java:525) 02-12 19:03:16.545:E / AndroidRuntime(986):在android.view.View $ 1.onClick(View.java:3628) 02-12 19:03:16.545:E / AndroidRuntime(986):... 11更多 02-12 19:03:16.545:E / AndroidRuntime(986):引起:android.database.CursorIndexOutOfBoundsException:请求索引0,大小为0 02-12 19:03:16.545:E / AndroidRuntime(986):在android.database.AbstractCursor.checkPosition(AbstractCursor.java:424) 02-12 19:03:16.545:E / AndroidRuntime(986):在android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) 02-12 19:03:16.545:E / AndroidRuntime(986):在android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50) 02-12 19:03:16.545:E / AndroidRuntime(986):at com.example.Bookstore.DatabaseHandler.findbook(DatabaseHandler.java:97) 02-12 19:03:16.545:E / AndroidRuntime(986):at com.example.Bookstore.MainActivity.SearchClickHandler(MainActivity.java:50) 02-12 19:03:16.545:E / AndroidRuntime(986):... 14更多
编辑2: 这是代码的新堆栈跟踪:
02-12 19:59:40.578:E / SpannableStringBuilder(1962):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度 02-12 20:00:28.037:D / AndroidRuntime(1962):关闭VM 02-12 20:00:28.057:W / dalvikvm(1962):threadid = 1:线程退出未捕获的异常 (组= 0x414c4700) 02-12 20:00:28.137:E / AndroidRuntime(1962):致命异常:主要 02-12 20:00:28.137:E / AndroidRuntime(1962):java.lang.IllegalStateException:无法执行方法 活动 02-12 20:00:28.137:E / AndroidRuntime(1962):在android.view.View $ 1.onClick(View.java:3633) 02-12 20:00:28.137:E / AndroidRuntime(1962):在android.view.View.performClick(View.java:4240) 02-12 20:00:28.137:E / AndroidRuntime(1962):在android.view.View $ PerformClick.run(View.java:17721) 02-12 20:00:28.137:E / AndroidRuntime(1962):在android.os.Handler.handleCallback(Handler.java:730) 02-12 20:00:28.137:E / AndroidRuntime(1962):在android.os.Handler.dispatchMessage(Handler.java:92) 02-12 20:00:28.137:E / AndroidRuntime(1962):在android.os.Looper.loop(Looper.java:137) 02-12 20:00:28.137:E / AndroidRuntime(1962):在android.app.ActivityThread.main(ActivityThread.java:5103) 02-12 20:00:28.137:E / AndroidRuntime(1962):at java.lang.reflect.Method.invokeNative(Native Method) 02-12 20:00:28.137:E / AndroidRuntime(1962):at java.lang.reflect.Method.invoke(Method.java:525) 02-12 20:00:28.137:E / AndroidRuntime(1962):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737) 02-12 20:00:28.137:E / AndroidRuntime(1962):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 02-12 20:00:28.137:E / AndroidRuntime(1962):at dalvik.system.NativeStart.main(Native Method) 02-12 20:00:28.137:E / AndroidRuntime(1962):引起:java.lang.reflect.InvocationTargetException 02-12 20:00:28.137:E / AndroidRuntime(1962):at java.lang.reflect.Method.invokeNative(Native Method) 02-12 20:00:28.137:E / AndroidRuntime(1962):at java.lang.reflect.Method.invoke(Method.java:525) 02-12 20:00:28.137:E / AndroidRuntime(1962):在android.view.View $ 1.onClick(View.java:3628) 02-12 20:00:28.137:E / AndroidRuntime(1962):... 11更多 02-12 20:00:28.137:E / AndroidRuntime(1962):引起:java.lang.NumberFormatException:无效的int: " BOOK1" 02-12 20:00:28.137:E / AndroidRuntime(1962):at java.lang.Integer.invalidInt(Integer.java:138) 02-12 20:00:28.137:E / AndroidRuntime(1962):at java.lang.Integer.parse(Integer.java:375) 02-12 20:00:28.137:E / AndroidRuntime(1962):at java.lang.Integer.parseInt(Integer.java:366) 02-12 20:00:28.137:E / AndroidRuntime(1962):at java.lang.Integer.parseInt(Integer.java:332) 02-12 20:00:28.137:E / AndroidRuntime(1962):at com.example.Bookstore.DatabaseHandler.findbook (DatabaseHandler.java:93) 02-12 20:00:28.137:E / AndroidRuntime(1962):at com.example.Bookstore.MainActivity.SearchClickHandler (MainActivity.java:50) 02-12 20:00:28.137:E / AndroidRuntime(1962):... 14更多
答案 0 :(得分:0)
android.database.CursorIndexOutOfBoundsException:请求索引0, 大小为0
当光标为空时,您正尝试访问游标中的第一个元素。您需要在if(cursor.moveToFirst())语句中创建并返回book对象,而不是在它之后。尝试将findbook方法更改为此。
public Books findbook(String name ) {
String query = "Select * FROM " + TABLE_CONTACTS + " WHERE " +
KEY_NAME + " = \"" + name + "\"";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()){
Books book = new Books();
book.setID(Integer.parseInt(cursor.getString(0)));
book.setName(cursor.getString(1));
book.setprice(Float.parseFloat(cursor.getString(2)));
return book;
}
return null;
}
您已经在活动中处理了null,这很好。