Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android-'android.database.CursorIndexOutOfBoundsException:请求索引0,大小为0'_Java_Android_Android Sqlite - Fatal编程技术网

Java android-'android.database.CursorIndexOutOfBoundsException:请求索引0,大小为0'

Java android-'android.database.CursorIndexOutOfBoundsException:请求索引0,大小为0',java,android,android-sqlite,Java,Android,Android Sqlite,我想从SQLite数据库中获取两列,并像Arraylist一样返回它,但我遇到了这个错误 05-12 03:31:37.763 15166-15166/com.example.abdullah.newstroynory E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.abdullah.newstroynory, PID: 15166 java.lang.RuntimeException: Unable to start a

我想从SQLite数据库中获取两列,并像Arraylist一样返回它,但我遇到了这个错误

05-12 03:31:37.763 15166-15166/com.example.abdullah.newstroynory E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.abdullah.newstroynory, PID: 15166
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.abdullah.newstroynory/com.example.abdullah.newstroynory.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:468)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at com.example.abdullah.newstroynory.SQLForMyStroies.getTitleAndImage(SQLForMyStroies.java:58)
at com.example.abdullah.newstroynory.MainActivity.onCreate(MainActivity.java:75)
我的错误

从我的SQlite类的这一行开始

forDoing.setTitle(cursor.getString(cursor.getColumnIndex("story_title")));
我的SQLiteOpenHelper类

我的主要活动课

我认为这是java代码中的逻辑错误

你能纠正我吗


我是android新手,对java不熟悉。错误是因为我的DB SQlite为空,因为每次我运行应用程序时,它都会为我创建一个新的DB

解决方案

所以我删除了我所有的DB SQLite,我把条件

我的条件是,如果我的数据库SQLite中有数据,请不要从internt获取数据

我的主要活动

我的SQLiteOpenHelper类

forDoing.setTitle(cursor.getString(cursor.getColumnIndex("story_title")));
public class SQLForMyStroies extends SQLiteOpenHelper {

    private static final int DB_VERSION = 1;
    private static final String DB_NAME = "newStoryNory5";



    public SQLForMyStroies(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE stories (story_id INTEGER PRIMARY KEY AUTOINCREMENT,story_title VARCHAR,story_image VARCHAR,story_body VARCHAR,story_audio VARCHAR )");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS stories");
        onCreate(db);
    }



    public boolean CreateNewStory( String t, String i, String b){
        SQLiteDatabase db= this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("story_title" , t);
        contentValues.put("story_image" , i);
        contentValues.put("story_body" , b);
        // contentValues.put("story_audio" , a); 
        db.insert("stories",null,contentValues);

        return true;
    }



        public ArrayList<StoryModel> getTitleAndImage(){

            ArrayList<StoryModel> myData = new ArrayList <> ();

            SQLiteDatabase db= this.getReadableDatabase();
            Cursor cursor =db.rawQuery("SELECT * FROM stories ",null);

            cursor.moveToFirst();
            do {

                StoryModel forDoing=new StoryModel();
                forDoing.setTitle(cursor.getString(cursor.getColumnIndex("story_title")));
                forDoing.setImage(cursor.getString(cursor.getColumnIndex("story_image")));

                myData.add(forDoing);

            }while (cursor.moveToNext());

            cursor.close();
            db.close();

            return(myData);
        }
ArrayList<StoryModel> TitleAndImage =mySQL.getTitleAndImage();

    for (StoryModel storyModel : TitleAndImage) {
        Log.d("story_title", storyModel.getTitle());
        Log.d("story_image", storyModel.getImage());
    }
    if(mySQL.checktitle (storyObject.getTitle().toString())) {
                            Log.d("--------------------", "data is exist ");
                            break;
                        }
      public  boolean checktitle ( String title){
            SQLiteDatabase db= this.getWritableDatabase();
            Cursor cur = db.rawQuery("SELECT * FROM stories where story_title='"+title+"'" ,null);
            if(cur.getCount()>0)
                return true;
            return false;
        }