本文介绍了IllegalStateException光标错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对不起,重复的问题,但我尝试使许多方式不起作用在applecation中,在第一轮,它可以推动通过活动。但是后来这是一个错误。
SQLiteDatabase mDb;
Database3 mHelper;
SQLiteDatabase mDb2;
数据库mHelper2;
private Cursor mCursor;
private String name;
private String image;
private String location;
private String info;
private String shop;
private String mcontact;
mHelper2 = new Database(this,shop);
mDb2 = mHelper2.getWritableDatabase();
mHelper2.onUpgrade(mDb2,1,1);
if(info.equalsIgnoreCase(null)){
AlertDialog.Builder builder = new AlertDialog.Builder(Activity_Mall_Floor.this);
builder.setTitle(楼层说明);
builder.setMessage(No Floor information of+ name);
builder.setNeutralButton(OK,null);
builder.show();
}
else {
ListView listView1 =(ListView)findViewById(R.id.listView2);
mHelper = new Database3(this,info);
mDb = mHelper.getWritableDatabase();
mHelper.onUpgrade(mDb,1,1);
mCursor = mDb.rawQuery(SELECT+ Database3.COL_FLOOR +,
+ Database3.COL_IMAGE +FROM+ Database3.TABLE_NAME,null);
ArrayList< String> dirArray = new ArrayList< String>();
mCursor.moveToFirst();
while(!mCursor.isAfterLast()){
dirArray.add(mCursor.getString
(mCursor.getColumnIndex(Database3.COL_FLOOR)));
mCursor.moveToNext();
}
ArrayAdapter< String> adapterDir =
new ArrayAdapter< String>(getApplicationContext()
,android.R.layout.simple_list_item_1,dirArray);
listView1.setAdapter(adapterDir);
listView1.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0,View arg1,int arg2,
long arg3){
mCursor.moveToPosition arg2);
String floorName = mCursor.getString(mCursor.getColumnIndex(Database3.COL_FLOOR));
String mapFloor = mCursor.getString(mCursor.getColumnIndex(Database3.COL_IMAGE));
String floor =;
if(floorName.equalsIgnoreCase(1st Floor))
floor =1;
else if(floorName.equalsIgnoreCase $ 2;
else if(floorName.equalsIgnoreCase(3rd Floor))
floor =3;
else
floor =4;
mCursor = mDb2.rawQuery(SELECT DISTINCT+ Database.COL_NAME
+FROM+ Database.TABLE_NAME
+WHERE+ Database.COL_FLOOR +='+ floor +',null);
ArrayList< String> dirArray = new ArrayList< String>();
mCursor.moveToFirst();
while(!mCursor.isAfterLast()){
dirArray.add(mCursor.getString
(mCursor.getColumnIndex(Database.COL_NAME)));
mCursor.moveToNext();
}
Intent intent = new Intent(getApplicationContext(),Activity_Mall_FMap.class);
Bundle bundle = new Bundle();
bundle.putString(Floorname,floorName);
bundle.putString(Mapfloor,mapFloor);
bundle.putString(Shop,shop);
intent.putExtra(FloorList,dirArray);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
错误表单logcat
02-12 01:31:46.917:E / AndroidRuntime(11370):FATAL EXCEPTION:main
02-12 01:31:46.917:E / AndroidRuntime(11370) :java.lang.IllegalStateException:无法从CursorWindow读取行0,col -1。在访问数据之前,确保光标正确初始化。
02-12 01:31:46.917:E / AndroidRuntime(11370):android.database.CursorWindow.nativeGetString(Native Method)
02-12 01:31:46.917:E / AndroidRuntime(11370 ):at android.database.CursorWindow.getString(CursorWindow.java:442)
02-12 01:31:46.917:E / AndroidRuntime(11370):在android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java: 51)
02-12 01:31:46.917:E / AndroidRuntime(11370):at com.test.mall.Activity_Mall_Floor $ 1.onItemClick(Activity_Mall_Floor.java:85)
02-12 01:31 :46.917:E / AndroidRuntime(11370):在android.widget.AdapterView.performItemClick(AdapterView.java:292)
02-12 01:31:46.917:E / AndroidRuntime(11370):在android.widget。 AbsListView.performItemClick(AbsListView.java:1068)
02-12 01:31:46.917:E / AndroidRuntime(11370):在android.widget.AbsListView $ PerformClick.run(AbsListView.java:2525)
这是错误行85
String floorName = mCursor.getString(m Cursor.getColumnIndex(Database3.COL_FLOOR));
String mapFloor = mCursor.getString(mCursor.getColumnIndex(Database3.COL_IMAGE));
感谢您的指导。
解决方案
修复:
- 尝试通过moveToFirst定位游标,然后从中读取数据。
-
关闭光标if if(c.moveToFirst()){}
-
检查null.e,g; if(c!= null&& c.moveToFirst()){}
-
检查count.e,g; (c!= null&& c.getCount()> 0&& c.moveToFirst()){}
Sorry for the repeat Question.but I try to make many way doesn't workIn applecation,In the first round, it can push through Activity. But back then it was a mistake.
SQLiteDatabase mDb;
Database3 mHelper;
SQLiteDatabase mDb2;
Database mHelper2;
private Cursor mCursor;
private String name;
private String image;
private String location;
private String info;
private String shop;
private String mcontact;
mHelper2 = new Database(this,shop);
mDb2 = mHelper2.getWritableDatabase();
mHelper2.onUpgrade(mDb2, 1, 1);
if (info.equalsIgnoreCase("null")){
AlertDialog.Builder builder = new AlertDialog.Builder(Activity_Mall_Floor.this);
builder.setTitle("Floor Description");
builder.setMessage("No Floor information of "+name);
builder.setNeutralButton("OK", null);
builder.show();
}
else {
ListView listView1 = (ListView)findViewById(R.id.listView2);
mHelper = new Database3(this,info);
mDb = mHelper.getWritableDatabase();
mHelper.onUpgrade(mDb, 1, 1);
mCursor = mDb.rawQuery("SELECT " + Database3.COL_FLOOR + ", "
+ Database3.COL_IMAGE+ " FROM " + Database3.TABLE_NAME, null);
ArrayList<String> dirArray = new ArrayList<String>();
mCursor.moveToFirst();
while ( !mCursor.isAfterLast() ){
dirArray.add(mCursor.getString
(mCursor.getColumnIndex(Database3.COL_FLOOR)));
mCursor.moveToNext();
}
ArrayAdapter<String> adapterDir =
new ArrayAdapter<String>(getApplicationContext()
, android.R.layout.simple_list_item_1, dirArray);
listView1.setAdapter(adapterDir);
listView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
mCursor.moveToPosition(arg2);
String floorName = mCursor.getString(mCursor.getColumnIndex(Database3.COL_FLOOR));
String mapFloor = mCursor.getString(mCursor.getColumnIndex(Database3.COL_IMAGE));
String floor = "";
if(floorName.equalsIgnoreCase("1st Floor"))
floor = "1";
else if (floorName.equalsIgnoreCase("2nd Floor"))
floor = "2";
else if (floorName.equalsIgnoreCase("3rd Floor"))
floor = "3";
else
floor = "4";
mCursor = mDb2.rawQuery("SELECT DISTINCT " + Database.COL_NAME
+" FROM " + Database.TABLE_NAME
+" WHERE " + Database.COL_FLOOR + " = '" + floor +"' ", null);
ArrayList<String> dirArray = new ArrayList<String>();
mCursor.moveToFirst();
while ( !mCursor.isAfterLast() ){
dirArray.add(mCursor.getString
(mCursor.getColumnIndex(Database.COL_NAME)));
mCursor.moveToNext();
}
Intent intent = new Intent(getApplicationContext(), Activity_Mall_FMap.class);
Bundle bundle = new Bundle();
bundle.putString("Floorname", floorName);
bundle.putString("Mapfloor", mapFloor);
bundle.putString("Shop", shop);
intent.putExtra("FloorList",dirArray);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
Error form logcat
02-12 01:31:46.917: E/AndroidRuntime(11370): FATAL EXCEPTION: main
02-12 01:31:46.917: E/AndroidRuntime(11370): java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
02-12 01:31:46.917: E/AndroidRuntime(11370): at android.database.CursorWindow.nativeGetString(Native Method)
02-12 01:31:46.917: E/AndroidRuntime(11370): at android.database.CursorWindow.getString(CursorWindow.java:442)
02-12 01:31:46.917: E/AndroidRuntime(11370): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
02-12 01:31:46.917: E/AndroidRuntime(11370): at com.test.mall.Activity_Mall_Floor$1.onItemClick(Activity_Mall_Floor.java:85)
02-12 01:31:46.917: E/AndroidRuntime(11370): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
02-12 01:31:46.917: E/AndroidRuntime(11370): at android.widget.AbsListView.performItemClick(AbsListView.java:1068)
02-12 01:31:46.917: E/AndroidRuntime(11370): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2525)
and this is error line 85
String floorName = mCursor.getString(mCursor.getColumnIndex(Database3.COL_FLOOR));
String mapFloor = mCursor.getString(mCursor.getColumnIndex(Database3.COL_IMAGE));
Thank you for Guidance.
解决方案
Fix:
Try to position cursor by moveToFirst before reading data from it.
Close the cursor after if ( c.moveToFirst()) {}
check for null.e,g; if (c != null && c.moveToFirst()) {}
check for count.e,g; (c != null && c.getCount() >0 && c.moveToFirst()){}
这篇关于IllegalStateException光标错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!