如何解决java.lang.IllegalStateException?
How to solve java.lang.IllegalStateException?
我正在制作一个应用程序,其中的数据是从 database.But 中检索的,我收到一条错误消息
java.lang.IllegalStateException: Couldn't read row 6, col 6 from
CursorWindow. Make sure the Cursor is initialized correctly before
accessing data from it.
请帮助我找到解决方案,因为我无法得到它。另外,我附上了 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;
}}
根据当前的实现 config_cursorWindowSize 支持最大 2 MB size.If 行大小超过则会抛出错误。
在 sqlite 数据库中存储大数据无论如何都不是一个好主意。将文件存储在文件系统中,将路径存储在数据库中。
在数据库中占有很大的份额——难怪它无法获取它。大于 2MB
从这里更改代码:
sqldb=help.getReadableDatabase();
sqldb=help.getWritableDatabase();
至:
sqldb=help.getWritableDatabase();
sqldb=help.getReadableDatabase();
希望这对您有所帮助。
openOrCreateDatabase("Qwerty", Context.MODE_PRIVATE, null);
你必须定义你保存的 sqlite 到 sd 卡的路径而不是 "Qwerty"。
我正在制作一个应用程序,其中的数据是从 database.But 中检索的,我收到一条错误消息
java.lang.IllegalStateException: Couldn't read row 6, col 6 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
请帮助我找到解决方案,因为我无法得到它。另外,我附上了 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;
}}
根据当前的实现 config_cursorWindowSize 支持最大 2 MB size.If 行大小超过则会抛出错误。
在 sqlite 数据库中存储大数据无论如何都不是一个好主意。将文件存储在文件系统中,将路径存储在数据库中。
在数据库中占有很大的份额——难怪它无法获取它。大于 2MB
从这里更改代码:
sqldb=help.getReadableDatabase();
sqldb=help.getWritableDatabase();
至:
sqldb=help.getWritableDatabase();
sqldb=help.getReadableDatabase();
希望这对您有所帮助。
openOrCreateDatabase("Qwerty", Context.MODE_PRIVATE, null);
你必须定义你保存的 sqlite 到 sd 卡的路径而不是 "Qwerty"。