访问SQLite数据库时CursorIndexOutOfBoundsException [英] CursorIndexOutOfBoundsException when accessing Sqlite database
问题描述
大家好,我一直有一些困难,希望你们可以帮忙。我试图创建一个小程序来存储我上大学的时间表。我一直有一些麻烦SQLite数据库虽然,它让我难住了。结果
当我第一次运行该程序就强制关闭
我得到了logcat的结果这个错误
android.database CursorIndexOutOfBoundsException:引起指数0请求。的大小为0
当我去打开创建再次表中的应用程序和应用程序工作正常。
我不知道为什么该表不被创建的第一次轮。结果
这里的code。结果
公共类扩展周一{ListActivity
INT POS = 0,timeInt,计数;
串[]次= {0900-1030,一零四零年至1220年,午餐1220至1325年,1325年至1455年,1500年至1630年};
的String [] []的内容;
字符串dayString =星期一,TIMESTRING;
的EditText EditTextLesson,EditTextLocation,EditTextTutor,EditTextNotes;
按钮btnSave,btnCancel,btnDelete;
对话框编辑;
意向变焦,mapweb;
所有的游标,的getRow;
MonDBAdapter周一;
DBAdapter分贝;
LV的ListView;
@覆盖
公共无效的onCreate(捆绑冰柱){
super.onCreate(冰柱);
的setContentView(R.layout.timetablelistview);
含量=新的String [10] [10];
编辑=新的对话框(Monday.this);
LV =(ListView控件)findViewById(android.R.id.list); lv.addFooterView(buildFooter());
变焦=新意图(这一点,Zoom.class);
mapweb =新意图(这一点,MapWebView.class); DB =新DBAdapter(本);
db.open();
周一=新MonDBAdapter(本); mon.open();
所有= mon.getAllEntries();
数= all.getCount(); 如果(计数4;){
的for(int i = 0;我小于5;我++){
mon.createEntry(,,,);
} }
addSections();}
公共无效onListItemClick(ListView的父母,视图V,INT位置,长的id){
开关(位置){
情况1:
timeInt = 1;
TIMESTRING =(0900-1030);
打破;
案例3:
timeInt = 2;
TIMESTRING =(1040年至1220年);
打破;
情况5:
timeInt = 3;
TIMESTRING =(1220年至1325年);
打破;
案例7:
timeInt = 4;
TIMESTRING =(1325年至1455年);
打破;
案例9:
timeInt = 5;
TIMESTRING =(1500至1630年);
打破;
} editDialog();
}SectionedAdapter适配器=新SectionedAdapter(){
受保护的视图getHeaderView(字符串标题,INT指数,
查看convertView,
父母的ViewGroup){
TextView的结果=(TextView中)convertView; 如果(convertView == NULL){
结果=(TextView中)getLayoutInflater()。膨胀(R.layout.header,
空值);
} result.setText(标题); 返回(结果);
}
};
公共无效addSections(){
all.moveToFirst();
的for(int i = 0;我小于5;我++){
getSectionContent(
倍[I]中,
all.getString(1),
all.getString(2),
all.getString(3),
all.getString(4));
all.moveToNext();
}
}公共无效getSectionContent(字符串时,字符串的教训,字符串位置,导师字符串,字符串注释){
//适配器将内容添加到每个部分
的String [] =由新的String [] {教训,地利,家教,注意事项};
INT []为= INT新[] {R.id.textViewLesson,R.id.textViewLocation,R.id.textViewTutor,R.id.textViewNotes};
// prepare的所有记录列表
清单<&HashMap的LT;字符串,字符串>> fillMaps =新的ArrayList<&HashMap的LT;字符串,字符串>>();
HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
map.put(教训,教训);
map.put(位置,位置);
map.put(导师,导师);
map.put(笔记,笔记);
fillMaps.add(地图);
SimpleAdapter适配器1 =新SimpleAdapter(这一点,fillMaps,R.layout.list_view_item,从,到); adapter.addSection(时间,适配器1);
lv.setAdapter(适配器);
}
公共无效editDialog(){
的getRow = mon.getEntry(timeInt);
edit.setTitle(编辑+ dayString ++ TIMESTRING);
edit.setContentView(R.layout.edit);
edit.setCancelable(真);
btnSave =(按钮)edit.findViewById(R.id.btnSave);
btnCancel =(按钮)edit.findViewById(R.id.btnCancel);
btnDelete =(按钮)edit.findViewById(R.id.btnDelete);
EditTextLesson =(EditText上)edit.findViewById(R.id.editTextLesson);
EditTextLocation =(EditText上)edit.findViewById(R.id.editTextLocation);
EditTextTutor =(EditText上)edit.findViewById(R.id.editTextTutor);
EditTextNotes =(EditText上)edit.findViewById(R.id.editTextNotes); EditTextLesson.setText(getRow.getString(1));
EditTextLocation.setText(getRow.getString(2));
EditTextTutor.setText(getRow.getString(3));
EditTextNotes.setText(getRow.getString(4)); btnCancel.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
edit.cancel(); }
}); btnSave.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
//从1开始为0包含部分时间
mon.updateEntry(timeInt,EditTextLesson.getText()的toString(),EditTextLocation.getText()的toString(),EditTextTutor.getText()的toString(),EditTextNotes.getText()的toString()。); all.requery(); adapter.sections.clear();
addSections();
edit.cancel();
}
});
btnDelete.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){ EditTextLesson.setText();
EditTextLocation.setText();
EditTextTutor.setText();
EditTextNotes.setText();
}
});
edit.show();
}私人查看buildFooter(){
按钮BTN =新按钮(本); btn.setText(校园地图);
btn.setTextSize(18);
btn.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
if(VERSION.SDK.equals(\"4\")||VERSION.SDK.equals(\"3\")||VERSION.SDK.equals(\"2\")||VERSION.SDK.equals(\"1\")){
startActivity(缩放);
}其他{
startActivity(mapweb);
} }
}); 返回(BTN);
} }
结果
公共类DBAdapter {公共静态最后弦乐DATABASE_NAME =时间表;
公共静态最终诠释DATABASE_VERSION = 1;私有静态最后弦乐星期一=
CREATE TABLE周一(_id整数主键自动增量
+ MonDBAdapter.LESSON +TEXT
+ MonDBAdapter.LOCATION +TEXT
+ MonDBAdapter.TUTOR +TEXT
+ MonDBAdapter.NOTES +TEXT+);;私有静态最后弦乐星期二=
CREATE TABLE周二(_id整数主键自动增量
+ MonDBAdapter.LESSON +TEXT
+ MonDBAdapter.LOCATION +TEXT
+ MonDBAdapter.TUTOR +TEXT
+ MonDBAdapter.NOTES +TEXT+);;私有静态最后弦乐星期三=
CREATE TABLE周三(_id整数主键自动增量
+ MonDBAdapter.LESSON +TEXT
+ MonDBAdapter.LOCATION +TEXT
+ MonDBAdapter.TUTOR +TEXT
+ MonDBAdapter.NOTES +TEXT+);;私有静态最后弦乐星期四=
CREATE TABLE周四(_id整数主键自动增量
+ MonDBAdapter.LESSON +TEXT
+ MonDBAdapter.LOCATION +TEXT
+ MonDBAdapter.TUTOR +TEXT
+ MonDBAdapter.NOTES +TEXT+);;私有静态最后弦乐星期五=使用
CREATE TABLE周五(_id整数主键自动增量
+ MonDBAdapter.LESSON +TEXT
+ MonDBAdapter.LOCATION +TEXT
+ MonDBAdapter.TUTOR +TEXT
+ MonDBAdapter.NOTES +TEXT+);;
私人最终上下文的背景下;
私人DatabaseHelper DBHelper;
私人SQLiteDatabase分贝;
公共DBAdapter(上下文CTX)
{
this.context = CTX;
this.DBHelper =新DatabaseHelper(this.context);
}私有静态类DatabaseHelper扩展SQLiteOpenHelper
{
DatabaseHelper(上下文的背景下)
{
超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
} @覆盖
公共无效的onCreate(SQLiteDatabase DB)
{
db.execSQL(星期一);
db.execSQL(星期二);
db.execSQL(星期三);
db.execSQL(星期四);
db.execSQL(星期五);
} @覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,
INT静态网页)
{ }
}
公共DBAdapter的open()抛出的SQLException
{
this.db = this.DBHelper.getWritableDatabase();
返回此;
}
公共无效的close()
{
this.DBHelper.close();
}
}
结果
公共类MonDBAdapter {
公共静态最后弦乐ROW_ID =_id;
公共静态最后弦乐课=名;
公共静态最后弦乐LOCATION =模特;
公共静态最后弦乐TUTOR =年;
公共静态最后弦乐NOTES =注意事项;私有静态最后弦乐星期一=星期一;私人DatabaseHelper mDbHelper;
私人SQLiteDatabase MDB;私人最终上下文mCtx;私有静态类DatabaseHelper扩展SQLiteOpenHelper { DatabaseHelper(上下文的背景下){
超(背景下,DBAdapter.DATABASE_NAME,空,DBAdapter.DATABASE_VERSION);
} @覆盖
公共无效的onCreate(SQLiteDatabase DB){
} @覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
}
}
公共MonDBAdapter(上下文CTX){
this.mCtx = CTX;
}
公共MonDBAdapter的open()抛出的SQLException {
this.mDbHelper =新DatabaseHelper(this.mCtx);
this.mDb = this.mDbHelper.getWritableDatabase();
返回此;
}
公共无效的close(){
this.mDbHelper.close();
}
众长createEntry(字符串教训,字符串位置,导师字符串,字符串注释){
ContentValues initialValues =新ContentValues();
initialValues.put(课课);
initialValues.put(位置,位置);
initialValues.put(导师,导师);
initialValues.put(注,注);
返回this.mDb.insert(星期一,空,initialValues);
}
公共布尔deleteEntry(长ROWID){ 返回this.mDb.delete(星期一,ROW_ID +=+ ROWID,NULL)> 0; // $ NON-NLS-1 $
}
公共光标getAllEntries(){ 返回this.mDb.query(星期一,新的String [] {ROW_ID,
课,地点,导师,NOTES},NULL,NULL,NULL,NULL,NULL);
}
公共光标getEntry(长ROWID)抛出的SQLException { 光标mCursor = this.mDb.query(真周一,新的String [] {ROW_ID,教训,
位置,博士生导师,NOTES},ROW_ID +=+ ROWID,NULL,NULL,NULL,NULL,NULL);
如果(mCursor!= NULL){
mCursor.moveToFirst();
}
返回mCursor;
}公共布尔updateEntry(长ROWID,串课,字符串位置,
家教字符串,字符串注释){
ContentValues ARGS =新ContentValues();
args.put(课课);
args.put(位置,位置);
args.put(导师,导师);
args.put(注,注);
返回this.mDb.update(星期一,ARGS,ROW_ID +=+ ROWID,NULL)大于0;
}
这是我得到什么在logcat中:
ERROR / AndroidRuntime(2464):致命异常:主要
ERROR / AndroidRuntime(2464):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.scotty65 / com.scotty65.TabBarExample}了java.lang.RuntimeException:无法启动活动ComponentInfo {com.scotty65 / com.scotty65。周一}:android.database.CursorIndexOutOfBoundsException:索引0要求,大小为0
ERROR / AndroidRuntime(2464):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
ERROR / AndroidRuntime(2464):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
ERROR / AndroidRuntime(2464):在android.app.ActivityThread.access $ 1500(ActivityThread.java:123)
ERROR / AndroidRuntime(2464):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:939)
ERROR / AndroidRuntime(2464):在android.os.Handler.dispatchMessage(Handler.java:99)
ERROR / AndroidRuntime(2464):在android.os.Looper.loop(Looper.java:123)
ERROR / AndroidRuntime(2464):在android.app.ActivityThread.main(ActivityThread.java:3835)
ERROR / AndroidRuntime(2464):在java.lang.reflect.Method.invokeNative(本机方法)
ERROR / AndroidRuntime(2464):在java.lang.reflect.Method.invoke(Method.java:507)
ERROR / AndroidRuntime(2464):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:841)
ERROR / AndroidRuntime(2464):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
/ AndroidRuntime(2464):在dalvik.system.NativeStart.main(本机方法)
ERROR / AndroidRuntime(2464):了java.lang.RuntimeException:产生的原因无法启动活动ComponentInfo {com.scotty65 / com.scotty65.Monday}:android.database.CursorIndexOutOfBoundsException:索引0要求,大小为0
ERROR / AndroidRuntime(2464):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
ERROR / AndroidRuntime(2464):在android.app.ActivityThread.startActivityNow(ActivityThread.java:1598)
ERROR / AndroidRuntime(2464):在android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
ERROR / AndroidRuntime(2464):在android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
如果您通过<一个文件了href=\"http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#getReadableDatabase%28%29\"相对=nofollow> getReadableDatabase ,它说:
像getWritableDatabase(),这个方法可能需要很长的时间来恢复,所以你不应该从应用程序主线程中调用它,包括ContentProvider.onCreate()
块引用>和我所看到的在你的程序中,首先要在DBAdapter的构造函数中调用这个
this.DBHelper =新DatabaseHelper(this.context);
this.DBHelper.getWritableDatabase();
this.DBHelper.getReadableDatabase();,然后在你的onCreate呼吁db.open();你在哪里再次调用this.DBHelper.getWritableDatabase();
所以,有可能你的数据库加载不正确。并尝试做后台线程前AsyncTask的这一切操作。
希望这帮助!
Hi everyone I’ve been having some difficulty, hope one of you can help. I’m trying to create a little program to store my timetable for college. I’ve been having some trouble with the SQlite database though, it’s got me stumped.
When I first run the program it force closes I get this error in logcat
Caused by: android.database CursorIndexOutOfBoundsException: Index 0 requested. With a size of 0 When I go to open the application again the table is created and the app works fine. I have no idea why the table is not being created the first time round.
Here’s the code.
public class Monday extends ListActivity { int pos = 0, timeInt, count; String[] times = {"0900-1030","1040-1220","Lunch 1220-1325","1325-1455", "1500-1630"}; String[][] content; String dayString = "Monday", timeString; EditText EditTextLesson, EditTextLocation, EditTextTutor, EditTextNotes; Button btnSave, btnCancel , btnDelete; Dialog edit; Intent zoom , mapweb; Cursor all, getRow; MonDBAdapter mon; DBAdapter db; ListView lv; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.timetablelistview); content= new String [10][10]; edit = new Dialog(Monday.this); lv = (ListView) findViewById(android.R.id.list); lv.addFooterView(buildFooter()); zoom =new Intent(this,Zoom.class); mapweb =new Intent(this,MapWebView.class); db = new DBAdapter(this); db.open(); mon = new MonDBAdapter(this); mon.open(); all = mon.getAllEntries(); count = all.getCount(); if(count < 4){ for(int i = 0; i < 5; i++){ mon.createEntry("", "", "", ""); } } addSections(); } public void onListItemClick(ListView parent, View v, int position, long id){ switch(position){ case 1: timeInt= 1; timeString=("0900-1030"); break; case 3: timeInt= 2; timeString=("1040-1220"); break; case 5: timeInt= 3; timeString=("1220-1325"); break; case 7: timeInt= 4; timeString=("1325-1455"); break; case 9: timeInt= 5; timeString=("1500-1630"); break; } editDialog(); } SectionedAdapter adapter=new SectionedAdapter() { protected View getHeaderView(String caption, int index, View convertView, ViewGroup parent) { TextView result=(TextView)convertView; if (convertView==null) { result=(TextView)getLayoutInflater().inflate(R.layout.header, null); } result.setText(caption); return(result); } }; public void addSections(){ all.moveToFirst(); for(int i = 0; i < 5; i++){ getSectionContent( times[i], all.getString(1), all.getString(2), all.getString(3), all.getString(4)); all.moveToNext(); } } public void getSectionContent(String time,String lesson,String location,String tutor,String notes){ //adapter to add content to each section String[] from = new String[] {"lesson", "location", "tutor", "notes"}; int[] to = new int[] {R.id.textViewLesson, R.id.textViewLocation, R.id.textViewTutor, R.id.textViewNotes }; // prepare the list of all records List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>(); HashMap<String, String> map = new HashMap<String, String>(); map.put("lesson", lesson ); map.put("location", location ); map.put("tutor", tutor ); map.put("notes", notes ); fillMaps.add(map); SimpleAdapter adapter1 = new SimpleAdapter(this, fillMaps, R.layout.list_view_item, from, to); adapter.addSection(time,adapter1); lv.setAdapter(adapter); } public void editDialog(){ getRow = mon.getEntry(timeInt); edit.setTitle("Edit "+ dayString + " " + timeString); edit.setContentView(R.layout.edit); edit.setCancelable(true); btnSave = (Button)edit.findViewById(R.id.btnSave); btnCancel = (Button)edit.findViewById(R.id.btnCancel); btnDelete = (Button)edit.findViewById(R.id.btnDelete); EditTextLesson=(EditText)edit.findViewById(R.id.editTextLesson); EditTextLocation=(EditText)edit.findViewById(R.id.editTextLocation); EditTextTutor=(EditText)edit.findViewById(R.id.editTextTutor); EditTextNotes=(EditText)edit.findViewById(R.id.editTextNotes); EditTextLesson.setText(getRow.getString(1)); EditTextLocation.setText(getRow.getString(2)); EditTextTutor.setText(getRow.getString(3)); EditTextNotes.setText(getRow.getString(4)); btnCancel.setOnClickListener(new OnClickListener() { public void onClick(View v) { edit.cancel(); } }); btnSave.setOnClickListener(new OnClickListener() { public void onClick(View v) { //starts at 1 as 0 contain the section time mon.updateEntry(timeInt,EditTextLesson.getText().toString(),EditTextLocation.getText().toString(), EditTextTutor.getText().toString(),EditTextNotes.getText().toString()); all.requery(); adapter.sections.clear(); addSections(); edit.cancel(); } }); btnDelete.setOnClickListener(new OnClickListener() { public void onClick(View v) { EditTextLesson.setText(""); EditTextLocation.setText(""); EditTextTutor.setText(""); EditTextNotes.setText(""); } }); edit.show(); } private View buildFooter() { Button btn=new Button(this); btn.setText("Campus map"); btn.setTextSize(18); btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if(VERSION.SDK.equals("4")||VERSION.SDK.equals("3")||VERSION.SDK.equals("2")||VERSION.SDK.equals("1")){ startActivity(zoom); }else{ startActivity(mapweb); } } }); return(btn); } }
public class DBAdapter { public static final String DATABASE_NAME = "timetable"; public static final int DATABASE_VERSION = 1; private static final String MONDAY = "create table monday (_id integer primary key autoincrement, " + MonDBAdapter.LESSON+ " TEXT," + MonDBAdapter.LOCATION+ " TEXT," + MonDBAdapter.TUTOR+ " TEXT," + MonDBAdapter.NOTES+ " TEXT" + ");"; private static final String TUESDAY = "create table tuesday (_id integer primary key autoincrement, " + MonDBAdapter.LESSON+ " TEXT," + MonDBAdapter.LOCATION+ " TEXT," + MonDBAdapter.TUTOR+ " TEXT," + MonDBAdapter.NOTES+ " TEXT" + ");"; private static final String WEDNESDAY = "create table wednesday (_id integer primary key autoincrement, " + MonDBAdapter.LESSON+ " TEXT," + MonDBAdapter.LOCATION+ " TEXT," + MonDBAdapter.TUTOR+ " TEXT," + MonDBAdapter.NOTES+ " TEXT" + ");"; private static final String THURSDAY = "create table thursday (_id integer primary key autoincrement, " + MonDBAdapter.LESSON+ " TEXT," + MonDBAdapter.LOCATION+ " TEXT," + MonDBAdapter.TUTOR+ " TEXT," + MonDBAdapter.NOTES+ " TEXT" + ");"; private static final String FRIDAY = "create table friday (_id integer primary key autoincrement, " + MonDBAdapter.LESSON+ " TEXT," + MonDBAdapter.LOCATION+ " TEXT," + MonDBAdapter.TUTOR+ " TEXT," + MonDBAdapter.NOTES+ " TEXT" + ");"; private final Context context; private DatabaseHelper DBHelper; private SQLiteDatabase db; public DBAdapter(Context ctx) { this.context = ctx; this.DBHelper = new DatabaseHelper(this.context); } private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(MONDAY); db.execSQL(TUESDAY); db.execSQL(WEDNESDAY); db.execSQL(THURSDAY); db.execSQL(FRIDAY); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } } public DBAdapter open() throws SQLException { this.db = this.DBHelper.getWritableDatabase(); return this; } public void close() { this.DBHelper.close(); } }
public class MonDBAdapter { public static final String ROW_ID = "_id"; public static final String LESSON = "name"; public static final String LOCATION = "model"; public static final String TUTOR = "year"; public static final String NOTES = "notes"; private static final String MONDAY= "monday"; private DatabaseHelper mDbHelper; private SQLiteDatabase mDb; private final Context mCtx; private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DBAdapter.DATABASE_NAME, null, DBAdapter.DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } } public MonDBAdapter(Context ctx) { this.mCtx = ctx; } public MonDBAdapter open() throws SQLException { this.mDbHelper = new DatabaseHelper(this.mCtx); this.mDb = this.mDbHelper.getWritableDatabase(); return this; } public void close() { this.mDbHelper.close(); } public long createEntry(String lesson, String location, String tutor , String notes){ ContentValues initialValues = new ContentValues(); initialValues.put(LESSON, lesson); initialValues.put(LOCATION, location); initialValues.put(TUTOR, tutor); initialValues.put(NOTES, notes); return this.mDb.insert(MONDAY, null, initialValues); } public boolean deleteEntry(long rowId) { return this.mDb.delete(MONDAY, ROW_ID + "=" + rowId, null) > 0; //$NON-NLS-1$ } public Cursor getAllEntries() { return this.mDb.query(MONDAY, new String[] { ROW_ID, LESSON, LOCATION, TUTOR, NOTES }, null, null, null, null, null); } public Cursor getEntry(long rowId) throws SQLException { Cursor mCursor = this.mDb.query(true, MONDAY, new String[] { ROW_ID, LESSON, LOCATION, TUTOR, NOTES}, ROW_ID + "=" + rowId, null, null, null, null, null); if (mCursor != null) { mCursor.moveToFirst(); } return mCursor; } public boolean updateEntry(long rowId, String lesson, String location, String tutor, String notes){ ContentValues args = new ContentValues(); args.put(LESSON, lesson); args.put(LOCATION, location); args.put(TUTOR, tutor); args.put(NOTES, notes); return this.mDb.update(MONDAY, args, ROW_ID + "=" + rowId, null) >0; }
Here's what I getting in logcat:
ERROR/AndroidRuntime(2464): FATAL EXCEPTION: main ERROR/AndroidRuntime(2464): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.scotty65/com.scotty65.TabBarExample}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.scotty65/com.scotty65.Monday}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 ERROR/AndroidRuntime(2464): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768) ERROR/AndroidRuntime(2464): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784) ERROR/AndroidRuntime(2464): at android.app.ActivityThread.access$1500(ActivityThread.java:123) ERROR/AndroidRuntime(2464): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939) ERROR/AndroidRuntime(2464): at android.os.Handler.dispatchMessage(Handler.java:99) ERROR/AndroidRuntime(2464): at android.os.Looper.loop(Looper.java:123) ERROR/AndroidRuntime(2464): at android.app.ActivityThread.main(ActivityThread.java:3835) ERROR/AndroidRuntime(2464): at java.lang.reflect.Method.invokeNative(Native Method) ERROR/AndroidRuntime(2464): at java.lang.reflect.Method.invoke(Method.java:507) ERROR/AndroidRuntime(2464): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) ERROR/AndroidRuntime(2464): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) /AndroidRuntime(2464): at dalvik.system.NativeStart.main(Native Method) ERROR/AndroidRuntime(2464): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.scotty65/com.scotty65.Monday}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 ERROR/AndroidRuntime(2464): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768) ERROR/AndroidRuntime(2464): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1598) ERROR/AndroidRuntime(2464): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127) ERROR/AndroidRuntime(2464): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
解决方案If you got through the documentation of getReadableDatabase, it says
Like getWritableDatabase(), this method may take a long time to return, so you should not call it from the application main thread, including from ContentProvider.onCreate()
And what I see in your program, first you are calling this in constructor of DBAdapter
this.DBHelper = new DatabaseHelper(this.context); this.DBHelper.getWritableDatabase(); this.DBHelper.getReadableDatabase();
and then in onCreate you are calling db.open(); where you are again calling this.DBHelper.getWritableDatabase();
So, it might be possible yours database is not properly loaded. And try to do all this operation in background thread ex AsyncTask.
Hope this help!!!
这篇关于访问SQLite数据库时CursorIndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!