I'm trying to put another item in the questions, but when I do so, an error appears and said about couldn't read row 0 col 6 from CursorWindow
I don't know what the problem is, it just forces the app to exit. Thanks.
public class CityAverageDbhelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "CityAverageDbhelper";
// tasks table name
private static final String TABLE_QUEST = "quest";
// tasks Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_QUES = "question";
private static final String KEY_ANSWER = "answer"; //correct option
private static final String KEY_OPTA= "opta"; //option a
private static final String KEY_OPTB= "optb"; //option b
private static final String KEY_OPTC= "optc"; //option c
private static final String KEY_OPTD= "optd"; //option d
private SQLiteDatabase dbase;
public CityAverageDbhelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
dbase=db;
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " ( "
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES
+ " TEXT, " + KEY_ANSWER+ " TEXT, "+KEY_OPTA +" TEXT, "
+KEY_OPTB +" TEXT, "+KEY_OPTC+" TEXT, "+KEY_OPTD+" TEXT)";
db.execSQL(sql);
addQuestions();
//db.close();
}
private void addQuestions()
{
Question q1=new Question("What is JP?","Jalur Pesawat", "Jack sParrow","asjdbajksd", "Jasa Programmer", "Jasa Programmer");
this.addQuestion(q1);
Question q2=new Question("where the JP place?", "Monas, Jakarta", "Gelondong, Bangun Tapan, bantul","asdasd", "Gelondong, Bangun Tapan, bandul", "Gelondong, Bangun Tapan, bantul");
this.addQuestion(q2);
Question q3=new Question("who is CEO of the JP?","Usman and Jack", "Jack and Rully","asdasdasd","Rully and Usman", "Rully and Usman" );
this.addQuestion(q3);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);
// Create tables again
onCreate(db);
}
// Adding new question
public void addQuestion(Question quest) {
//SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_QUES, quest.getQUESTION());
values.put(KEY_ANSWER, quest.getANSWER());
values.put(KEY_OPTA, quest.getOPTA());
values.put(KEY_OPTB, quest.getOPTB());
values.put(KEY_OPTC, quest.getOPTC());
values.put(KEY_OPTD, quest.getOPTD());
// Inserting Row
dbase.insert(TABLE_QUEST, null, values);
}
public List<Question> getAllQuestions() {
List<Question> quesList = new ArrayList<Question>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
dbase=this.getReadableDatabase();
Cursor cursor = dbase.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Question quest = new Question();
quest.setID(cursor.getInt(0));
quest.setQUESTION(cursor.getString(1));
quest.setANSWER(cursor.getString(2));
quest.setOPTA(cursor.getString(3));
quest.setOPTB(cursor.getString(4));
quest.setOPTC(cursor.getString(5));
quest.setOPTD(cursor.getString(6));
quesList.add(quest);
} while (cursor.moveToNext());
}
// return quest list
return quesList;
}
public int rowcount()
{
int row=0;
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
row=cursor.getCount();
return row;
}
}
The QuizActivity
public class CityAverageQuestion extends ActionBarActivity {
int min = 1;
int max = 49;
Random r = new Random();
int qid = r.nextInt(max - min + 1) + min;
List<Question> quesList;
int score=0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc, rdd;
Button butNext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.cityaveragequestion);
CityAverageDbhelper db=new CityAverageDbhelper(this);
quesList=db.getAllQuestions();
currentQ=quesList.get(qid);
txtQuestion=(TextView)findViewById(R.id.textView2);
rda=(RadioButton)findViewById(R.id.radio1);
rdb=(RadioButton)findViewById(R.id.radio2);
rdc=(RadioButton)findViewById(R.id.radio3);
rdd=(RadioButton)findViewById(R.id.radio5);
butNext=(Button)findViewById(R.id.button1);
setQuestionView();
//butNext.setOnClickListener(new View.OnClickListener() {
//}
}
public void onClick(View v) {
RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
Log.d("yourans", currentQ.getANSWER()+" "+answer.getText());
if(currentQ.getANSWER().equals(answer.getText()))
{
currentQ=quesList.get(qid);
setQuestionView();
Toast.makeText(getBaseContext(), "Correct Answer !!", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getBaseContext(), "Wrong Answer !!", Toast.LENGTH_LONG).show();
Intent intent = new Intent(CityAverageQuestion.this, CityAverage.class);
startActivity(intent);
finish();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.city_average_question, menu);
return true;
}
private void setQuestionView()
{
txtQuestion.setText(currentQ.getQUESTION());
rda.setText(currentQ.getOPTA());
rdb.setText(currentQ.getOPTB());
rdc.setText(currentQ.getOPTC());
rdd.setText(currentQ.getOPTD());
qid++;
}
}
here is the logcat
03-02 01:22:11.687: E/CursorWindow(18317): Failed to read row 0, column 6 from a CursorWindow which has 3 rows, 6 columns.
03-02 01:22:11.688: D/AndroidRuntime(18317): Shutting down VM
03-02 01:22:11.688: W/dalvikvm(18317): threadid=1: thread exiting with uncaught exception (group=0x4199ec98)
03-02 01:22:11.688: W/dalvikvm(18317): threadid=1: uncaught exception occurred
03-02 01:22:11.688: W/System.err(18317): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.quizexplorer/com.example.quizexplorer.CityAverageQuestion}: java.lang.IllegalStateException: Couldn't read row 0, col 6 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
03-02 01:22:11.688: W/System.err(18317): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
03-02 01:22:11.688: W/System.err(18317): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
03-02 01:22:11.689: W/System.err(18317): at android.app.ActivityThread.access$800(ActivityThread.java:151)
03-02 01:22:11.689: W/System.err(18317): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
03-02 01:22:11.689: W/System.err(18317): at android.os.Handler.dispatchMessage(Handler.java:110)
03-02 01:22:11.689: W/System.err(18317): at android.os.Looper.loop(Looper.java:193)
03-02 01:22:11.689: W/System.err(18317): at android.app.ActivityThread.main(ActivityThread.java:5292)
03-02 01:22:11.689: W/System.err(18317): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 01:22:11.689: W/System.err(18317): at java.lang.reflect.Method.invoke(Method.java:515)
03-02 01:22:11.689: W/System.err(18317): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
03-02 01:22:11.689: W/System.err(18317): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
03-02 01:22:11.689: W/System.err(18317): at dalvik.system.NativeStart.main(Native Method)
03-02 01:22:11.689: W/System.err(18317): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 6 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
03-02 01:22:11.690: W/System.err(18317): at android.database.CursorWindow.nativeGetString(Native Method)
03-02 01:22:11.690: W/System.err(18317): at android.database.CursorWindow.getString(CursorWindow.java:434)
03-02 01:22:11.690: W/System.err(18317): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:62)
03-02 01:22:11.690: W/System.err(18317): at com.example.quizexplorer.CityAverageDbhelper.getAllQuestions(CityAverageDbhelper.java:247)
03-02 01:22:11.690: W/System.err(18317): at com.example.quizexplorer.CityAverageQuestion.onCreate(CityAverageQuestion.java:46)
03-02 01:22:11.690: W/System.err(18317): at android.app.Activity.performCreate(Activity.java:5264)
03-02 01:22:11.690: W/System.err(18317): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
03-02 01:22:11.690: W/System.err(18317): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
03-02 01:22:11.691: W/System.err(18317): ... 11 more
Aucun commentaire:
Enregistrer un commentaire