如何解决“ java.lang.IllegalStateException:无法从CursorWindow中读取第2行,第7列。”

时间:2019-01-29 04:22:22

标签: android-sqlite

当我使用CursorWindow将数据库从SQLite插入到Music Adapter中时,它将报告错误 “ java.lang.IllegalStateException:无法从CursorWindow读取第0行,第7列。在访问游标之前,请确保已正确初始化了游标。”

这是针对Android Studio 3.3的。过去,我尝试过将数据从SQlite插入和导出到ArrayAdapter进行Listview的操作,并且经常发生错误: “ java.lang.IllegalStateException:无法从CursorWindow读取行0,第7列”

这是我的代码:

public class MusicAdapter extends ArrayAdapter<Music>
{
    Activity context;
    int resource;
    List<Music> objects;

     int Like =0; 

    public MusicAdapter(Activity context, int resource, List<Music> objects)
    {
        super(context, resource, objects);
        this.context = context;
        this.resource = resource;
        this.objects = objects;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = this.context.getLayoutInflater();
        View row = inflater.inflate(this.resource,null);

        TextView txtMa = row.<TextView>findViewById(R.id.txtMa);
        TextView txtTen = row.<TextView>findViewById(R.id.txtTen);
        TextView txtCaSi = row.<TextView>findViewById(R.id.txtCaSi);

        final TextView txtLike = row.<TextView>findViewById(R.id.txtLike);        final TextView txtDisLike = row.<TextView>findViewById(R.id.txtDisLike); 
        ImageButton btnLike = row.<ImageButton>findViewById(R.id.btnLike);
        ImageButton btnDisLike = row.<ImageButton>findViewById(R.id.btnDisLike);

        final Music music = this.objects.get(position);
        txtTen.setText(music.getTen());
        txtMa.setText(music.getMa());
        txtCaSi.setText(music.getCaSi());


        btnLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                xuLyThich(music, position,txtLike);

            }
        });

        btnDisLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                xuLyKhongThich(music,position,txtDisLike);

            }
        });

        return row;
    }

    private void xuLyKhongThich(Music music, int pos,TextView txtDisLike)
    {

        int no_un_like =0;
        Cursor cursor=MainActivity.database.query("ArirangSongList",null,
                null,null,
                null,null,null);


        try {
            if (cursor!= null) {
                cursor.move(pos+1);
                no_un_like = cursor.getInt(8);
                Log.d("no_unlike",String.valueOf(no_un_like));
            }

        } finally {
            cursor.close();
        }

        ContentValues row = new ContentValues();

        row.put("Dislike",  no_un_like+1);


        try{
            MainActivity.database.update("ArirangSongList", row, "MABH= ?", new String[]{String.valueOf(music.getMa())});
            txtDisLike.setText(String.valueOf(no_un_like+1));
        }finally {

        }
    }

    private void xuLyThich(Music music, int pos,TextView txtlike)
    {
         int no_like =0;
        Cursor cursor=MainActivity.database.query("ArirangSongList",null,
                null,null,
                null,null,null);


        try {
            if (cursor!= null) {
                cursor.move(pos+1);
                no_like = cursor.getInt(7);
                Log.d("no_like",String.valueOf(no_like));
            }

        } finally {
            cursor.close();
        }

        ContentValues row = new ContentValues();

        row.put("Like",  no_like+1);


        try{
            MainActivity.database.update("ArirangSongList", row, "MABH= ?", new String[]{String.valueOf(music.getMa())});

            txtlike.setText(String.valueOf(no_like+1));

        }finally {

        }
    }

}

这是我的错误:

java.lang.IllegalStateException: Couldn't read row 2, col 7 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
        at android.database.CursorWindow.nativeGetLong(Native Method)
        at android.database.CursorWindow.getLong(CursorWindow.java:507)
        at android.database.CursorWindow.getInt(CursorWindow.java:574)
        at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:69)
        at muitenvang.adapter.MusicAdapter.xuLyThich(MusicAdapter.java:136)
        at muitenvang.adapter.MusicAdapter.access$000(MusicAdapter.java:23)
        at muitenvang.adapter.MusicAdapter$1.onClick(MusicAdapter.java:74)
        at android.view.View.performClick(View.java:4204)
        at android.view.View$PerformClick.run(View.java:17355)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您的问题是,您正在尝试从游标中读取不存在的行。

这是由于列表中的位置不是偏移量而是序列号。那就是第一个位置是1,而光标中的等效行将是0(位置2将与行1相关,依此类推)。

根据cursor.move(pos+1);在位置上加1会使异常更有可能发生,因为不检查移动的结果(如果无法进行移动则为false,否则为true)看看移动是否成功。

检查从SQLiteDatabase方法返回的Cursor(例如 query fir null)是没有用的,因为Cursor不会为null。如果没有行,则Cursor仍然有效,但是使用Cursor getCount 方法检查的计数将返回0(或Cursor中的行数)。

尽管变化并不理想:-

        if (cursor!= null) {
            cursor.move(pos+1);
            no_like = cursor.getInt(7);
            Log.d("no_like",String.valueOf(no_like));
        }

收件人:-

        if (cursor.move(pos-1) {
            no_like = cursor.getInt(7);
            Log.d("no_like",String.valueOf(no_like));
        } else {
            Log.d("no_like","Ooops could not move to row + String.valueOf(pos));
        }

更有可能工作。

  • 请注意,应将相同的更改应用于 xuLyKhongThich