我如何解决这个SQLite java.lang.IllegalStateException:无法读取CursorWindow中的第0行col -1

问题描述 投票:-2回答:2

FATAL EXCEPTION:main进程:example.myproject,PID:2608 java.lang.RuntimeException:无法启动活动ComponentInfo {edgedev.andelaproject / example.myproject.Activites_and_Fragments.MainActivity}:java.lang.IllegalStateException:无法读取第0行,来自CursorWindow的col -1。在从中访问数据之前,请确保正确初始化Cursor。

在MyDB.class(SQLiteOpenHelper的子类)中:

这是我的表格声明

String create_table = "CREATE TABLE my_table_of_profiles (profile_id INTEGER, username TEXT, profile TEXT, image TEXT, score TEXT ) "; 

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(create_table);
}

在我的DatabaseAccessObject.class中:

public ArrayList<Profile> getEntriesFromDB(Context context) {

    SQLiteDatabase db = new MyDB(context).getWritableDatabase();

    Cursor cursor = db.query("my_table_of_profiles" , null, null, null, null, null, null);
    Profile profile;
    ArrayList<Profile> profiles = new ArrayList<>();


    while (cursor.moveToNext()) {

        /*Here is ->  DatabaseAccessObject.java:69*/ int a = cursor.getInt(cursor.getColumnIndex("profile_id");
        String b= cursor.getString(cursor.getColumnIndex("username");
        String c= cursor.getString(cursor.getColumnIndex("profile");
        String d= cursor.getString(cursor.getColumnIndex("image");
        Double e = Double.parseDouble(cursor.getString(cursor.getColumnIndex("score");


        profile = new Profile(a,b,c,d,e );
        profiles.add(profile);
    }
    cursor.close();
    db.close();
    return profiles;
}
public boolean storeProfiles (Context context, ArrayList<Profile> profile) {
            SQLiteDatabase db = new MyDB(context).getWritableDatabase();
            db.beginTransaction();
for(Profile freshProfile : profile){

 ContentValues cv = new ContentValues();
                    cv.put("id", freshProfile.getProfile_id());
                    cv.put("username", freshProfile.getProfile_username());
                    cv.put("profile", freshProfile.getProfile_url());
                    cv.put("image", freshProfile.getImage_url());
                    cv.put("score", freshProfile.getScore());

                    long result = db.insert("my_table_of_profiles", null, cv);

                    if (result < 0) {
                        return false;
                    }
}
            db.setTransactionSuccessful();
            db.endTransaction();
            db.close();
        return true;
    }

Profile.class

public class Profile {
private int profile_id;
private String profile_username;
private String profile_url;
private String image_url;
private Double score;


public Profile(int profile_id, String profile_username, String profile_url, String image_url, Double score ) {

    this.image_url = image_url;
    this.profile_username = profile_username;
    this.profile_url = profile_url;
    this.profile_id = profile_id;
    this.score = score;

}

public String getImage_url() {
    return image_url;
}

public String profile_username() {
    return profile_username;
}

public String getProfile_url() {
    return profile_url;
}

public int getProfile_id() {
    return profile_id;
}

public String getScore() {
    return ""+round(score);
}

private double round(double value){
    long factor = (long) Math.pow(10,3);
    value = value * factor;
    long tmp = Math.round(value);

    return (double) tmp/factor;
}

}

在MainActivity.java(Inside OnCreate方法)中,我放了一些虚拟数据:

Profile profile;
ArrayList<Profile> profiles = new ArrayList<>();

    for (int i=0 ; i<20; i++){
        profile = new Profile(i,"username"+i, "http:///www.url.com"+i,"imgur.com/abcdefgh", 1.234);

        profiles.add(profile);
    }

    new DatabaseAccessObject().storePosts(this, profiles);
ArrayList<Profile> retrieved = DatabaseAccessObject.getsInstance().getEntriesFromDB(this);
// i also tried to retrieve the items from the database
//This is where i got an Error 

我得到的错误摘录:

引起:java.lang.IllegalStateException:无法从CursorWindow读取第0行,col -1。在从中访问数据之前,请确保正确初始化Cursor。在android.database.AursWindowedCursor.getInt的android.database.CursorWindow.getLong(CursorWindow.java:511)上的android.database.CursorWindow.getInt(CursorWindow.java:578)的android.database.CursorWindow.nativeGetLong(Native Method) (AbstractWindowedCursor.java:69)at edgedev.andelaproject.Database.DatabaseAccessEntriesFromDB(DatabaseAccessObject.java:69)at edgedev.andelaproject.Database.DatabaseAccessObject.storePosts(DatabaseAccessObject.java:26)at edgedev.andelaproject.Activites_and_Fragments.MainActivity。 onCreate(MainActivity.java:71)在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)的android.app.Activity.performCreate(Activity.java:6237)

android sqlite android-sqlite android-database
2个回答
2
投票
int a = cursor.getInt(cursor.getColumnIndex("id");

你没有列id

它应该是profile_id

int a = cursor.getInt(cursor.getColumnIndex("profile_id");

进行此更改后,请不要忘记重新安装应用程序。


0
投票

引起:java.lang.IllegalStateException:无法从CursorWindow读取第0行,col -1。在访问之前,请确保正确初始化Cursor

今天在Android工作室/ Kotlin项目中发生在我身上

问题是

我正在插入

col1 col2 col3 col4 col5

但在其他类中我只有一个arrayOf(col1 col2 col3)(相同的数据库)添加col4 col5解决了我知道的问题并不是直接的答案只是为了说明与windowcursor和.movetofirst无关的错误本身

希望这有点帮助

© www.soinside.com 2019 - 2024. All rights reserved.