我有一个活动,在两个列表视图中显示搜索结果(在我的数据库中)。问题是每次运行应用程序时应用程序总是崩溃。这就是logcat向我展示的内容:
07-02 12:42:15.940: E/CursorWindow(23645): Bad request for field slot 0,1. numRows = 10, numColumns = 1
07-02 12:42:15.980: E/AndroidRuntime(23645): FATAL EXCEPTION: main
07-02 12:42:15.980: E/AndroidRuntime(23645): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.series3/com.example.series3.FindMePlace}: java.lang.IllegalStateException: get field slot from row 0 col 1 failed
07-02 12:42:15.980: E/AndroidRuntime(23645): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-02 12:42:15.980: E/AndroidRuntime(23645): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-02 12:42:15.980: E/AndroidRuntime(23645): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-02 12:42:15.980: E/AndroidRuntime(23645): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-02 12:42:15.980: E/AndroidRuntime(23645): at android.os.Handler.dispatchMessage(Handler.java:99)
07-02 12:42:15.980: E/AndroidRuntime(23645): at android.os.Looper.loop(Looper.java:123)
07-02 12:42:15.980: E/AndroidRuntime(23645): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-02 12:42:15.980: E/AndroidRuntime(23645): at java.lang.reflect.Method.invokeNative(Native Method)
07-02 12:42:15.980: E/AndroidRuntime(23645): at java.lang.reflect.Method.invoke(Method.java:507)
07-02 12:42:15.980: E/AndroidRuntime(23645): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-02 12:42:15.980: E/AndroidRuntime(23645): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-02 12:42:15.980: E/AndroidRuntime(23645): at dalvik.system.NativeStart.main(Native Method)
07-02 12:42:15.980: E/AndroidRuntime(23645): Caused by: java.lang.IllegalStateException: get field slot from row 0 col 1 failed
07-02 12:42:15.980: E/AndroidRuntime(23645): at android.database.CursorWindow.getString_native(Native Method)
07-02 12:42:15.980: E/AndroidRuntime(23645): at android.database.CursorWindow.getString(CursorWindow.java:329)
07-02 12:42:15.980: E/AndroidRuntime(23645): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
07-02 12:42:15.980: E/AndroidRuntime(23645): at com.example.series3.DatabaseHandler.getAllFromLocation(DatabaseHandler.java:737)
07-02 12:42:15.980: E/AndroidRuntime(23645): at com.example.series3.FindMePlace.populateFromLocation(FindMePlace.java:108)
07-02 12:42:15.980: E/AndroidRuntime(23645): at com.example.series3.FindMePlace.onCreate(FindMePlace.java:53)
07-02 12:42:15.980: E/AndroidRuntime(23645): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-02 12:42:15.980: E/AndroidRuntime(23645): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
这是我的代码的一部分。
FindMePlace.java
private void populateToDestination() {
List<UkmRoute> destination = db.getAllToDestination();
for (UkmRoute k : destination) {
results.add(k.getToDestination());
results_id.add(k.getID());
}
if (!filterText2.getText().toString().equals(""))
adapter = new ArrayAdapter<String>(FindMePlace.this,
R.layout.list_item, R.id.textView1, results);
else {
ArrayList<String> r = new ArrayList<String>();
adapter = new ArrayAdapter<String>(FindMePlace.this,
R.layout.list_item, R.id.textView1, r);
}
setListAdapter(adapter);
// Set ListView from ListActivity
ListView lv2 = getListView();
lv2.setTextFilterEnabled(true);
// Set click event from listView
lv2.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();
Log.d("test", "position:" + position);
Log.d("test",
"actualname:"
+ db.getUkmRouteByTo(adapter.getItem(position))
.getFromLocation());
// String poiID = results_id.get(position);
String poiID = db.getUkmRouteByTo(adapter.getItem(position))
.getID();
setSelectedPoi(poiID);
// startActivity(c);
}
});
}
private void populateFromLocation() {
// Reading location
Log.d("Reading", "Reading all location ..");
List<UkmRoute> location = db.getAllFromLocation();
for (UkmRoute k : location) {
results.add(k.getFromLocation());
results_id.add(k.getID());
}
// Set list arrayAdapter to adapter
if (!filterText.getText().toString().equals(""))
adapter = new ArrayAdapter<String>(FindMePlace.this,
R.layout.list_item, R.id.textView1, results);
else {
ArrayList<String> r = new ArrayList<String>();
adapter = new ArrayAdapter<String>(FindMePlace.this,
R.layout.list_item, R.id.textView1, r);
}
// adapter = new ArrayAdapter<String>(this, R.layout.list_item,
// R.id.textView1, results);
setListAdapter(adapter);
// Set ListView from ListActivity
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();
Log.d("test", "position:" + position);
Log.d("test",
"actualname:"
+ db.getUkmRouteByFrom(
adapter.getItem(position))
.getFromLocation());
// String poiID = results_id.get(position);
String poiID = db.getUkmRouteByFrom(adapter.getItem(position))
.getID();
setSelectedPoi(poiID);
// startActivity(c);
}
});
}
DatabaseHandler.java
public List<UkmRoute> getAllToDestination() {
List<UkmRoute> ToList = new ArrayList<UkmRoute>();
//Select All Query
String selectQuery = "SELECT DISTINCT ToDestination FROM " + TABLE_UKMROUTE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
//looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
UkmRoute poi = new UkmRoute();
poi.setID(cursor.getString(0));
poi.setFromLocation(cursor.getString(1));
poi.setToDestination(cursor.getString(2));
poi.setRoute1(cursor.getString(3));
poi.setRoute2(cursor.getString(4));
//Adding kategori to list
ToList.add(poi);
} while (cursor.moveToNext());
}
// return Kategori list
return ToList;
}
public List<UkmRoute> getAllFromLocation() {
List<UkmRoute> ToList = new ArrayList<UkmRoute>();
//Select All Query
String selectQuery = "SELECT DISTINCT FromLocation FROM " + TABLE_UKMROUTE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
//looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
UkmRoute poi = new UkmRoute();
poi.setID(cursor.getString(0));
poi.setFromLocation(cursor.getString(1));
poi.setToDestination(cursor.getString(2));
poi.setRoute1(cursor.getString(3));
poi.setRoute2(cursor.getString(4));
//Adding kategori to list
ToList.add(poi);
} while (cursor.moveToNext());
}
// return Kategori list
return ToList;
}
我的SQL查询有什么问题吗?
String selectQuery = "SELECT DISTINCT ToDestination FROM " + TABLE_UKMROUTE;
答案 0 :(得分:0)
您的查询ToDestination
中只有一列。
但是你试图从多个专栏中获取信息:
poi.setID(cursor.getString(0));
poi.setFromLocation(cursor.getString(1));
poi.setToDestination(cursor.getString(2));
poi.setRoute1(cursor.getString(3));
poi.setRoute2(cursor.getString(4));
试试这个:
"SELECT DISTINCT ID, FromLocation, ToDestination, Route1, Route2 FROM " + TABLE_UKMROUTE;
或
"SELECT DISTINCT * FROM " + TABLE_UKMROUTE;
和
poi.setID(cursor.getInteger("ID")); // getInteger only if ID is an Integer
poi.setFromLocation(cursor.getString("FromLocation"));
poi.setToDestination(cursor.getString("ToDestination"));
poi.setRoute1(cursor.getString("Route1"));
poi.setRoute2(cursor.getString("Route2"));