嗨,我不知道如何修复此游标错误,“java.lang.IllegalStateException:无法从 CursorWindow 读取第 0 行,第 1 列。”

Posted

技术标签:

【中文标题】嗨,我不知道如何修复此游标错误,“java.lang.IllegalStateException:无法从 CursorWindow 读取第 0 行,第 1 列。”【英文标题】:Hi, I can't figure out how to fix this cursor error, "java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow." 【发布时间】:2019-09-13 00:08:26 【问题描述】:

我对使用 android studio 还是很陌生,我不确定我什至可以做我想要在这里运行的事情。 我正在尝试运行从同一个表中提取的一些列表视图,当程序运行时,它会在这部分崩溃,说明光标。有什么想法吗?

日志猫

    --------- beginning of crash
04-23 19:18:55.749 3100-3100/com.example.j3 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.j3, PID: 3100
    java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.j3/com.example.j3.GroupList2: java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
        at android.database.CursorWindow.nativeGetString(Native Method)
        at android.database.CursorWindow.getString(CursorWindow.java:438)
        at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
        at com.example.j3.GroupList2.onViewData(GroupList2.java:61)
        at com.example.j3.GroupList2.onCreate(GroupList2.java:38)
        at android.app.Activity.performCreate(Activity.java:6237)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

主要代码

package com.example.j3;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;

public class GroupList2 extends AppCompatActivity 
    ListView listView, listView2, listView3, listView4;
    SQLiteDatabase db;
    Button clear;
    ArrayList<String> listname, listLearningstyle;



    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_list);

        clear = (Button) findViewById(R.id.clear);
        listView = (ListView) findViewById(R.id.lv1);
        listView2 = (ListView) findViewById(R.id.lv2);
        listView3 = (ListView) findViewById(R.id.lv3);
        listView4 = (ListView) findViewById(R.id.lv4);



        db = openOrCreateDatabase("Mydata", Context.MODE_PRIVATE, null);

        onViewData();
        onViewData2();
        onViewData3();
        onViewData4();

        clear.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                deleteAll();
                openStudents();
            
        );

    

    public void onViewData() 

         listname = new ArrayList<String>();
         listLearningstyle = new ArrayList<String>();
        listname.clear();
        listLearningstyle.clear();
        Cursor  c = db.rawQuery("select  NAME from id where LEARNINGSTYLE ='Visual' or LEARNINGSTYLE='VA' order by NAME desc, LEARNINGSTYLE desc ", null);
        while (c.moveToNext()) 
            listname.add(c.getString(1));
            listLearningstyle.add(c.getString(2));
        
        listView.setAdapter(new AdapterList2(GroupList2.this, listname, listLearningstyle) 
        );
     //shows the 1st list view

    public void onViewData2() 


         listname = new ArrayList<String>();
         listLearningstyle = new ArrayList<String>();
        listname.clear();
        listLearningstyle.clear();
        Cursor  c = db.rawQuery("select NAME from id where LEARNINGSTYLE ='Kinesthetic' or LEARNINGSTYLE='VK' order by NAME desc, LEARNINGSTYLE desc  ", null);
        while (c.moveToNext()) 
            listname.add(c.getString(1));
            listLearningstyle.add(c.getString(2));
        

        listView2.setAdapter(new AdapterList2(GroupList2.this, listname, listLearningstyle) 
        );
     //shows the 2nd list view

    public void onViewData3() 

         listname = new ArrayList<String>();
         listLearningstyle = new ArrayList<String>();
        listname.clear();
        listLearningstyle.clear();
        Cursor  c = db.rawQuery("select NAME from id where LEARNINGSTYLE ='Aural' or LEARNINGSTYLE='VA' order by NAME desc, LEARNINGSTYLE desc ", null);
        while (c.moveToNext()) 
            listname.add(c.getString(1));
            listLearningstyle.add(c.getString(2));
        
        listView3.setAdapter(new AdapterList2(GroupList2.this, listname, listLearningstyle) 
        );
     //shows the 3rd list view

    public void onViewData4() 

         listname = new ArrayList<String>();
         listLearningstyle = new ArrayList<String>();
        listname.clear();
        listLearningstyle.clear();
        Cursor   c = db.rawQuery("select NAME from id where LEARNINGSTYLE ='VAK' order by NAME desc, LEARNINGSTYLE desc ", null);
        while (c.moveToNext()) 
            listname.add(c.getString(1));
            listLearningstyle.add(c.getString(2));
        
        listView4.setAdapter(new AdapterList2(GroupList2.this, listname, listLearningstyle) 
        );

     //shows the 4th list view


    public void deleteAll() 
        db.delete("id", null, null); //delete all rows in a table
    

    public void openStudents() 
        Intent intent = new Intent(this, Students.class);
        startActivity(intent);
    



适配器列表

package com.example.j3;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

public class AdapterList2 extends BaseAdapter 
    ArrayList<String> n, p;
    GroupList2 l;
    LayoutInflater inflater;

    public AdapterList2(GroupList2 data, ArrayList<String> name, ArrayList<String> learningstyle) 
        this.l = data;
        this.n = name;
        this.p = learningstyle;
        this.inflater = LayoutInflater.from(data);
    

    @Override    public int getCount() 
        return n.size();
    

    @Override public Object getItem(int i) 
        return n.get(i);
    

    @Override    public long getItemId(int i) 
        return i;
    

    @Override    public View getView(int i, View view, ViewGroup viewGroup) 
        view=inflater.inflate(R.layout.adapter,viewGroup,false);
        TextView tvname=(TextView)view.findViewById(R.id.tvName);
        TextView tvlearningstyle=(TextView)view.findViewById(R.id.tvlearningstyle);
        tvname.setText(n.get(i));
        tvlearningstyle.setText(p.get(i));
        return view;
    


适配器 xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvName"
        android:layout_
        android:layout_
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:layout_weight="1"
        android:text="Name"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.016"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.14" />

    <TextView
        android:id="@+id/tvlearningstyle"
        android:layout_
        android:layout_
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:layout_weight="1"
        android:text="Name"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.055"
        app:layout_constraintStart_toEndOf="@+id/tvName"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.14" />

</android.support.constraint.ConstraintLayout>

我这几天一直在尝试解决这个问题,我希望能提供一些意见。

【问题讨论】:

【参考方案1】:

在您的“主代码”中,有如下几行:

Cursor  c = db.rawQuery("select  NAME from id where LEARNINGSTYLE ='Visual' 
or LEARNINGSTYLE='VA' order by NAME desc, LEARNINGSTYLE desc ", null);

仅选择 1 列:NAME 从表 id(这真的是表名吗?)。 然后是这样的行:

listname.add(c.getString(1));
listLearningstyle.add(c.getString(2));

Cursor 的列索引是基于0,也就是说第一列的索引是0,第二列的索引是1,但是你尝试访问12! 但是,请记住,查询只获取了 1 列。 改为:

Cursor  c = db.rawQuery("select NAME, LEARNINGSTYLE from id where LEARNINGSTYLE ='Visual' 
or LEARNINGSTYLE='VA' order by NAME desc, LEARNINGSTYLE desc ", null);

此查询获取 2 列:NAMELEARNINGSTYLE。 并将值添加到列表中,如下所示:

..................................................
listname.add(c.getString(0));
listLearningstyle.add(c.getString(1));

【讨论】:

您好,谢谢您的回复。是的,解决了这个错误,但现在我有了一个新错误:(原因:java.lang.NullPointerException:尝试在在 com.example.j3.GroupList2.onCreate(GroupList2.java:41) 的 com.example.j3.GroupList2.onViewData4(GroupList2.java:111) 处的空对象引用 这篇文章回答了你的问题。对于任何其他问题,您必须使用所有相关代码发布另一个问题。

以上是关于嗨,我不知道如何修复此游标错误,“java.lang.IllegalStateException:无法从 CursorWindow 读取第 0 行,第 1 列。”的主要内容,如果未能解决你的问题,请参考以下文章

如何使用嵌入 webhook 的 discord.py 修复此错误

如何在 Wamp_Server 中修复此错误? [关闭]

错误 ORA-01000: 超出最大打开游标

如何修复 Vuetify 安装 npm 错误 E404

如何调用返回引用游标的Oracle存储过程

如何修复此错误在多个渲染()操作期间无法使用相同的画布