如何解决java.lang.IllegalStateException?

时间:2017-08-04 10:45:08

标签: java android cursor

我正在创建一个应用程序,其中从数据库中检索数据。但是我收到一条错误,上面写着

  

java.lang.IllegalStateException:无法读取第6行,第6列   CursorWindow。确保之前正确初始化了Cursor   从中访问数据。

请帮助我找到解决方案,因为我无法得到它。我也附加了Java文件。

public class QuizActivity extends AppCompatActivity {
TextView tv, tv1;
RadioGroup rg;
Button btn;
SQLiteDatabase sqldb;
Cursor c;
int score, nscore,question;
String n;
int array[]=new int[11];
private static final String x="SELECT * FROM questions";

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quiz_activity);
    tv = (TextView) findViewById(R.id.textView4);
    tv1 = (TextView) findViewById(R.id.textView9);
    rg = (RadioGroup) findViewById(R.id.rg);
    btn = (Button) findViewById(R.id.button2);
    Bundle data = getIntent().getExtras();
    if (data!=null){
        n = data.getString("Name:");
    }
    openDatabase();
    c = sqldb.rawQuery(x,null);
    c.moveToFirst();
    getQuestion();
    tv1.setText("Score:0");
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int res = rg.getCheckedRadioButtonId();
            switch (res) {
                case 0:
                case 1:
                case 2:
                case 3:
                    break;
                default:
                    Toast.makeText(getApplicationContext(), "Select atleast 
      anyone option!", Toast.LENGTH_LONG).show();
                    return;
            }
            if (res == Integer.parseInt(c.getString(6))) {
                Toast.makeText(getApplicationContext(), "Correct answer!", Toast.LENGTH_LONG).show();
                score += nscore;
                tv1.setText("Score:" + nscore);
            } else {
                Toast.makeText(getApplicationContext(), "Incorrect answer!", Toast.LENGTH_LONG).show();
            }
            Toast.makeText(getApplicationContext(), "You have completed your quiz!", Toast.LENGTH_LONG).show();
            Intent i = new Intent(getApplicationContext(), FinialActivity.class);
            i.putExtra("Name:", n);
            i.putExtra("Score:", score);
            startActivity(i);
        }
    });
}


protected void openDatabase() {
    sqldb = openOrCreateDatabase("Qwerty", Context.MODE_PRIVATE, null);
}
protected ArrayList<String>getQuestion() {
    final String Table_name="questions";
    String SelectQuery="SELECT  * FROM "+Table_name;
    MyHelper help = new MyHelper(getApplicationContext());
    sqldb=help.getReadableDatabase();
    sqldb=help.getWritableDatabase();
    c=sqldb.rawQuery(SelectQuery,null);
    ArrayList<String> data= new ArrayList<>();
    if (c !=null){
        if (c.moveToFirst()){
            for (int i=0;i<c.getCount();i++){
                data.add(c.getString(i));
                c.moveToNext();
            }
        }
        c.close();
    }
    return data;
}}

3 个答案:

答案 0 :(得分:0)

根据当前的实现,config_ cursorWindowSize 支持最大2 MB大小。如果行大小超过则会抛出错误。

无论如何,在sqlite数据库中存储大数据并不是一个好主意。将文件存储在文件系统和数据库中的路径中。

拥有一个庞大的数据库 - 难怪它无法获取它。大于2MB

答案 1 :(得分:0)

更改此代码:

sqldb=help.getReadableDatabase();
 sqldb=help.getWritableDatabase();

为:

sqldb=help.getWritableDatabase();
 sqldb=help.getReadableDatabase();

希望这会帮助你。

答案 2 :(得分:0)

openOrCreateDatabase(“Qwerty”,Context.MODE_PRIVATE,null);

你必须将保存的sqlite的路径定义为sd-card而不是“Qwerty”。