尝试从sqlite数据库中获取spinner中的数据时,我收到了java.lang.IllegalStateException。数据在数据库中正确输入,当我试图检索一切正常。但是,当我尝试在微调器中加载数据时出现问题。 我的代码打算做的是首先在sqlite数据库中保存从web服务获得的数据,并为用户选择的国家加载状态。 如果你有更好的方法来做这个plz建议我的链接。以下是LogCat
10-06 01:08:38.125: E/CursorWindow(1067): Failed to read row 0, column 1 from a CursorWindow which has 1356 rows, 1 columns.
10-06 01:08:38.125: D/AndroidRuntime(1067): Shutting down VM
10-06 01:08:38.135: W/dalvikvm(1067): threadid=1: thread exiting with uncaught exception (group=0xb2a3bba8)
10-06 01:08:38.165: E/AndroidRuntime(1067): FATAL EXCEPTION: main
10-06 01:08:38.165: E/AndroidRuntime(1067): Process: com.manish.tabdemo, PID: 1067
10-06 01:08:38.165: E/AndroidRuntime(1067): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.manish.tabdemo/com.manish.tabdemo.countrystatelist}: java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
10-06 01:08:38.165: E/AndroidRuntime(1067): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
10-06 01:08:38.165: E/AndroidRuntime(1067): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-06 01:08:38.165: E/AndroidRuntime(1067): at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-06 01:08:38.165: E/AndroidRuntime(1067): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-06 01:08:38.165: E/AndroidRuntime(1067): at android.os.Handler.dispatchMessage(Handler.java:102)
10-06 01:08:38.165: E/AndroidRuntime(1067): at android.os.Looper.loop(Looper.java:136)
10-06 01:08:38.165: E/AndroidRuntime(1067): at android.app.ActivityThread.main(ActivityThread.java:5017)
10-06 01:08:38.165: E/AndroidRuntime(1067): at java.lang.reflect.Method.invokeNative(Native Method)
10-06 01:08:38.165: E/AndroidRuntime(1067): at java.lang.reflect.Method.invoke(Method.java:515)
10-06 01:08:38.165: E/AndroidRuntime(1067): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-06 01:08:38.165: E/AndroidRuntime(1067): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-06 01:08:38.165: E/AndroidRuntime(1067): at dalvik.system.NativeStart.main(Native Method)
10-06 01:08:38.165: E/AndroidRuntime(1067): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
10-06 01:08:38.165: E/AndroidRuntime(1067): at android.database.CursorWindow.nativeGetString(Native Method)
10-06 01:08:38.165: E/AndroidRuntime(1067): at android.database.CursorWindow.getString(CursorWindow.java:434)
10-06 01:08:38.165: E/AndroidRuntime(1067): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
10-06 01:08:38.165: E/AndroidRuntime(1067): at com.manish.tabdemo.DatabaseHandler.getAllCountries(DatabaseHandler.java:85)
10-06 01:08:38.165: E/AndroidRuntime(1067): at com.manish.tabdemo.countrystatelist.loadCountrySpinnerData(countrystatelist.java:80)
10-06 01:08:38.165: E/AndroidRuntime(1067): at com.manish.tabdemo.countrystatelist.onCreate(countrystatelist.java:48)
10-06 01:08:38.165: E/AndroidRuntime(1067): at android.app.Activity.performCreate(Activity.java:5231)
10-06 01:08:38.165: E/AndroidRuntime(1067): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-06 01:08:38.165: E/AndroidRuntime(1067): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
10-06 01:08:38.165: E/AndroidRuntime(1067): ... 11 more
10-06 01:09:21.635: I/Process(1067): Sending signal. PID: 1067 SIG: 9
以下是代码
countrystatelist.java
public class countrystatelist extends Activity implements OnItemSelectedListener{
private static final String NAMESPACE = "riderapp";
private static final String METHOD_NAME = "getCountryandStateList";
private static final String URL = "http://demo.php.otssolutions.com/ridersapp/admin/server.php?wsdl";
private static final String SOAP_ACTION = "riderappAppServer";
String Status;
String array;
String temp;
TextView textView;
String truee = "true";
DatabaseHandler db;
Spinner country_spinner;
Spinner state_spinner;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.countrystatelist);
textView = (TextView) findViewById(R.id.result);
country_spinner = (Spinner) findViewById(R.id.country);
state_spinner = (Spinner) findViewById(R.id.state);
country_spinner.setOnItemSelectedListener(this);
loadCountrySpinnerData();
AsyncCallWS task = new AsyncCallWS();
task.execute();
db = new DatabaseHandler(this);
}
public void callservice(){
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty(truee, "true");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try{
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject)envelope.bodyIn;
Status = result.getProperty(0).toString();
}
catch (Exception e) {
e.printStackTrace();
}
}
private void loadCountrySpinnerData() {
// database handler
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
// Spinner Drop down elements
List<countrystate> lables = db.getAllCountries();
// Creating adapter for spinner
ArrayAdapter<countrystate> dataAdapter = new ArrayAdapter<countrystate>(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
country_spinner.setAdapter(dataAdapter);
}
private void loadStateSpinnerData(String label) {
// database handler
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
// Spinner Drop down elements
List<countrystate> lables = db.getAllStates(label);
// Creating adapter for spinner
ArrayAdapter<countrystate> dataAdapter = new ArrayAdapter<countrystate>(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
state_spinner.setAdapter(dataAdapter);
}
private class AsyncCallWS extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
Log.i("OTS+++++++++++++++++++++++++++++++++++++++", "doInBackground");
callservice();
return null;
}
@Override
protected void onPostExecute(Void result) {
Log.i("OTS---------------------------------------", "onPostExecute");
//textView.setText(Status);
String s[] = new String[350];
String country_id[] = new String[350];
String country_name[] = new String[350];
String state_id[] = new String[700];
String state_name[] = new String[700];
StringTokenizer tokens = new StringTokenizer(Status, ",");
for(int i=0;i<339;i++){
s[i] = tokens.nextToken();
}
for(int i=0;i<339;i++){
country_id[i] = s[i].substring(s[i].indexOf("id=")+3, s[i].indexOf(";", s[i].indexOf("id=")));
country_name[i] = s[i].substring(s[i].indexOf("country_name=")+13, s[i].indexOf(";", s[i].indexOf("country_name=")));
state_id[i] = s[i].substring(s[i].indexOf("sid=")+4, s[i].indexOf(";", s[i].indexOf("sid=")));
state_name[i] = s[i].substring(s[i].indexOf("state_name=")+11, s[i].indexOf(";", s[i].indexOf("state_name")));
db.addEntry(new countrystate(country_id[i], country_name[i], state_id[i], state_name[i]));
}
List<countrystate> list = db.getAllCountries();
for( countrystate t : list){
Log.e("OTS", t.get_id()+t.get_countryname()+t.get_stateid()+t.get_statename());
}
}
@Override
protected void onPreExecute() {
Log.i("OTS===========================================", "onPreExecute");
textView.setText("Validating..Please Wait");
}
@Override
protected void onProgressUpdate(Void... values) {
Log.i("OTS*******************************************", "onProgressUpdate");
}
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String label = arg0.getItemAtPosition(arg2).toString();
loadStateSpinnerData(label);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
DatabaseHandler.java
公共类DatabaseHandler扩展了SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "countrystate";
private static final String TABLE = "countrystatelist";
private static final String Id = "id";
private static final String country_name = "countryname";
private static final String state_id = "stateid";
private static final String state_name = "statename";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE " + TABLE + "("
+ Id + " INTEGER ," + country_name + " TEXT,"
+ state_id + " INTEGER," + state_name + " TEXT" + ")";
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE);
// Create tables again
onCreate(db);
}
void addEntry(countrystate cs) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Id, cs.get_id());
values.put(country_name, cs.get_countryname());
values.put(state_id, cs.get_stateid());
values.put(state_name, cs.get_statename());
// Inserting Row
db.insert(TABLE, null, values);
db.close(); // Closing database connection
}
countrystate getoneentry(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE, new String[] { Id,
country_name , state_id, state_name }, Id + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
countrystate cs = new countrystate(cursor.getString(0),
cursor.getString(1),cursor.getString(2),cursor.getString(3));
return cs;
}
public List<countrystate> getAllCountries() {
List<countrystate> list = new ArrayList<countrystate>();
// Select All Query
String selectQuery = "SELECT " + country_name +" FROM " + TABLE ;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
countrystate cs = new countrystate();
cs.set_id(cursor.getString(0));
cs.set_countryname(cursor.getString(1));
cs.set_stateid(cursor.getString(2));
cs.set_statename(cursor.getString(3));
list.add(cs);
} while (cursor.moveToNext());
}
// return contact list
return list;
}
public List<countrystate> getAllStates(String c) {
List<countrystate> list = new ArrayList<countrystate>();
// Select All Query
String selectQuery = "SELECT " + state_name + " FROM " + TABLE + " WHERE " + Id + " = " + c;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
countrystate cs = new countrystate();
cs.set_id(cursor.getString(0));
cs.set_countryname(cursor.getString(1));
cs.set_stateid(cursor.getString(2));
cs.set_statename(cursor.getString(3));
list.add(cs);
} while (cursor.moveToNext());
}
// return contact list
return list;
}
public int getEntryCount() {
String countQuery = "SELECT * FROM " + TABLE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
}
countrystate.java
public class countrystate{
String id;
String country_name;
String state_id;
String state_name;
public countrystate(){
}
public countrystate(String id,String country_name,String state_id,String state_name){
this.id = id;
this.country_name = country_name;
this.state_id = state_id;
this.state_name = state_name;
}
public String get_id(){
return this.id;
}
public String get_countryname(){
return this.country_name;
}
public String get_stateid(){
return this.state_id;
}
public String get_statename(){
return this.state_name;
}
public void set_id(String id){
this.id = id;
}
public void set_countryname(String country_name){
this.country_name = country_name;
}
public void set_stateid(String state_id){
this.state_id = state_id;
}
public void set_statename(String state_name){
this.state_name = state_name;
}
}
答案 0 :(得分:1)
在getAllCountries()
中,您的SELECT
只有一列,但您尝试使用getString(0)
... getString(3)
访问4列中的数据。确保您的SELECT
至少选择了多列。
答案 1 :(得分:1)
10-06 01:08:38.125:E / CursorWindow(1067):无法读取第0行,列 来自CursorWindow的1,它有1356行,1列。
和
10-06 01:08:38.165:E / AndroidRuntime(1067):引起: java.lang.IllegalStateException:无法读取第0行,第1列 CursorWindow。确保之前正确初始化了Cursor 从中访问数据。
从此异常中,您可以了解此异常背后的原因。此异常表示您正在尝试读取不存在的第1列,因为只创建了一个列。
<强>建议:强>
请勿直接使用列号,最佳做法是使用Cursor.getColumnIndex(String columnName)方法。
不要使用单独的数组,最佳做法是使用ArrayList,其中object可以是任何用户定义的对象,如学生,学院,国家等。
不良做法:
String s [] = new String [350]; String country_id [] = new String [350]; String country_name [] = new String [350]; String state_id [] = new String [700]; String state_name [] = new String [700];
最佳做法:
ArrayList<Country> listCountry = new ArrayList<>();
Country objCountry = new Country();
objCountry.setCountryID(1);
objCountry.setCountryName("India");
listCountry.add(objCountry);