我在Android应用程序中使用SQLite数据库。我只想在spinner中检索一列。当我运行应用程序时,应用程序崩溃。我的日志猫显示:
字段插槽0,1的错误请求。 numRows = 1,numColumns = 1
以下是从数据库中检索数据并将其放入微调器(NewleadActivity.class)的代码。
这是我的Adapter类
public class DbHelper extends SQLiteOpenHelper
{
// Database Version
@SuppressWarnings("unused")
private static final int DATABASE_VERSION = 1;
// Database Name
static String DATABASE_NAME="define_products_database";
// Labels table name
public static final String TABLE_DEFINE_PRODUCT_NAME="define_products";
// Labels Table Columns names
public static final String KEY_PRODUCTS_NAME="product_name";
public static final String KEY_PRODUCTS_DETAILS="products_details";
public static final String KEY_ID="id";
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE="CREATE TABLE "+TABLE_DEFINE_PRODUCT_NAME+" ("+KEY_ID+" INTEGER PRIMARY KEY, "+KEY_PRODUCTS_NAME+" TEXT, "+KEY_PRODUCTS_DETAILS+" TEXT)";
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_DEFINE_PRODUCT_NAME);
onCreate(db);
}
/**
* Getting all labels
* returns list of labels
* */
public List<String> getAllLabels(){
List<String> labels = new ArrayList<String>();
// Select All Query
String selectQuery = " SELECT " + KEY_PRODUCTS_NAME + " FROM "+ TABLE_DEFINE_PRODUCT_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(1));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning lables
return labels;
}
}
这是包含spiner的活动代码
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.new_lead_activity5);
spinner_Products=(Spinner)findViewById(R.id.products_Spinner);
loadSpinnerData();
}
private void loadSpinnerData() {
// database handler
DbHelper db = new DbHelper(getApplicationContext());
// Spinner Drop down elements
List<String> lables = db.getAllLabels();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner_Products.setAdapter(dataAdapter);
}
}
这是我的Log cat。
11-27 11:28:28.548: E/AndroidRuntime(2150): FATAL EXCEPTION: main
11-27 11:28:28.548: E/AndroidRuntime(2150): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lead_management_project/com.lead_management_project.New_Lead_Activity5}: java.lang.IllegalStateException: get field slot from row 0 col 1 failed
11-27 11:28:28.548: E/AndroidRuntime(2150): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-27 11:28:28.548: E/AndroidRuntime(2150): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-27 11:28:28.548: E/AndroidRuntime(2150): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-27 11:28:28.548: E/AndroidRuntime(2150): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-27 11:28:28.548: E/AndroidRuntime(2150): at android.os.Handler.dispatchMessage(Handler.java:99)
11-27 11:28:28.548: E/AndroidRuntime(2150): at android.os.Looper.loop(Looper.java:123)
11-27 11:28:28.548: E/AndroidRuntime(2150): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-27 11:28:28.548: E/AndroidRuntime(2150): at java.lang.reflect.Method.invokeNative(Native Method)
11-27 11:28:28.548: E/AndroidRuntime(2150): at java.lang.reflect.Method.invoke(Method.java:507)
11-27 11:28:28.548: E/AndroidRuntime(2150): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-27 11:28:28.548: E/AndroidRuntime(2150): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-27 11:28:28.548: E/AndroidRuntime(2150): at dalvik.system.NativeStart.main(Native Method)
11-27 11:28:28.548: E/AndroidRuntime(2150): Caused by: java.lang.IllegalStateException: get field slot from row 0 col 1 failed
11-27 11:28:28.548: E/AndroidRuntime(2150): at android.database.CursorWindow.getString_native(Native Method)
11-27 11:28:28.548: E/AndroidRuntime(2150): at android.database.CursorWindow.getString(CursorWindow.java:329)
11-27 11:28:28.548: E/AndroidRuntime(2150): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
11-27 11:28:28.548: E/AndroidRuntime(2150): at com.db_mgmt.DbHelper.getAllLabels(DbHelper.java:65)
11-27 11:28:28.548: E/AndroidRuntime(2150): at com.lead_management_project.New_Lead_Activity5.loadSpinnerData(New_Lead_Activity5.java:123)
11-27 11:28:28.548: E/AndroidRuntime(2150): at com.lead_management_project.New_Lead_Activity5.onCreate(New_Lead_Activity5.java:80)
11-27 11:28:28.548: E/AndroidRuntime(2150): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-27 11:28:28.548: E/AndroidRuntime(2150): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-27 11:28:28.548: E/AndroidRuntime(2150): ... 11 more
答案 0 :(得分:1)
伙计,你只是在你的陈述中查询单列并请求第二列,只需使用以下方法,我只将Index从1改为零。
public List<String> getAllLabels(){
List<String> labels = new ArrayList<String>();
// Select All Query
String selectQuery = " SELECT " + KEY_PRODUCTS_NAME + " FROM "+ TABLE_DEFINE_PRODUCT_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(0));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning lables
return labels;
}