When i go to Test Level 1 activity, it stop working and unfortunately stopped working.
My Exception log is here:
10-30 15:14:14.871: E/AndroidRuntime(821): FATAL EXCEPTION: main
10-30 15:14:14.871: E/AndroidRuntime(821): java.lang.RuntimeException: Unable to start activity ComponentInfo{priscillia.benkyo/priscillia.benkyo.TestLevel1}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
10-30 15:14:14.871: E/AndroidRuntime(821): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-30 15:14:14.871: E/AndroidRuntime(821): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-30 15:14:14.871: E/AndroidRuntime(821): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-30 15:14:14.871: E/AndroidRuntime(821): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-30 15:14:14.871: E/AndroidRuntime(821): at android.os.Handler.dispatchMessage(Handler.java:99)
10-30 15:14:14.871: E/AndroidRuntime(821): at android.os.Looper.loop(Looper.java:137)
10-30 15:14:14.871: E/AndroidRuntime(821): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-30 15:14:14.871: E/AndroidRuntime(821): at java.lang.reflect.Method.invokeNative(Native Method)
10-30 15:14:14.871: E/AndroidRuntime(821): at java.lang.reflect.Method.invoke(Method.java:511)
10-30 15:14:14.871: E/AndroidRuntime(821): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-30 15:14:14.871: E/AndroidRuntime(821): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-30 15:14:14.871: E/AndroidRuntime(821): at dalvik.system.NativeStart.main(Native Method)
10-30 15:14:14.871: E/AndroidRuntime(821): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
10-30 15:14:14.871: E/AndroidRuntime(821): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
10-30 15:14:14.871: E/AndroidRuntime(821): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
10-30 15:14:14.871: E/AndroidRuntime(821): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
10-30 15:14:14.871: E/AndroidRuntime(821): at priscillia.benkyo.TestLevel1.displayQuestion(TestLevel1.java:193)
10-30 15:14:14.871: E/AndroidRuntime(821): at priscillia.benkyo.TestLevel1.onCreate(TestLevel1.java:147)
10-30 15:14:14.871: E/AndroidRuntime(821): at android.app.Activity.performCreate(Activity.java:5104)
10-30 15:14:14.871: E/AndroidRuntime(821): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-30 15:14:14.871: E/AndroidRuntime(821): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
I using SQLite Expert Personal 3 and i save in asset folder My DatabaseHelper
public class DatabaseHelper extends SQLiteOpenHelper{
private static String DB_PATH = "/data/data/priscillia.benkyo/databases/";
private static String DB_NAME = "Benkyou.db";
private static String Table_name="Soal";
private SQLiteDatabase myDataBase;
private SQLiteDatabase myData;
private final Context myContext;
/**
* Constructor
* Takes and keeps a reference of the passed context in order to access to the application assets and resources.
* @param context
*/
public DatabaseHelper(Context context) {
super(context, DB_NAME,null,1);
this.myContext = context;
}
//create empty database on the system and rewrites it with your own database
public void createDatabase() throws IOException {
boolean dbExist = checkDatabase();
if(dbExist) {
//do nothing (database already exist)
} else {
copyFiles();
}
}
private void copyFiles() {
try {
InputStream is = myContext.getAssets().open(DB_NAME);
File outfile = new File(DB_PATH,DB_NAME);
outfile.getParentFile().mkdirs();
outfile.createNewFile();
if (is==null) {
throw new RuntimeException("stream is null!");
} else {
FileOutputStream out = new FileOutputStream(outfile);
//BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outfile));
byte buf[] = new byte[1024];
do {
int numread = is.read(buf);
if(numread<=0) break;
out.write(buf, 0, numread);
}
while (true); is.close();
out.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/* Check if the database already exist to avoid re-copying the file each time you open the
application. * @return true if it exists, false if it doesn't */
private boolean checkDatabase() {
boolean checkDB = false;
try{
String myPath = myContext.getFilesDir().getAbsolutePath().
replace("files", "databases")+File.separator + DB_NAME;
File dbfile = new File(myPath);
checkDB = dbfile.exists();
}
catch(SQLiteException e){
System.out.println("Database doesn't exist");
}
return checkDB;
}
/*Copies your database from your local assets-folder to the just created empty database in
the system folder, from where it can be accessed and handled. This is done by transfering
byte stream.*/
private void copyDatabase() throws IOException{
//Open your local db as the input stream InputStream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db String
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLiteException{
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
/// Get Soal content////////
public Cursor getSoal_Content(int soalId)
{
String myPath = DB_PATH + DB_NAME;
myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
Cursor cur;
cur=myData.rawQuery("select soal_desc from Soal where soal_id='"+soalId+"'",null);
cur.moveToFirst();
myData.close();
return cur;
};
/// Get Soal content///
public Cursor getSoal_List()
{
String myPath = DB_PATH + DB_NAME;
myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
int i;
Cursor cur;
cur=myData.rawQuery("select soal_id,soal_desc,jawaban_corr from Soal",null);
cur.moveToFirst();
i = cur.getCount();
myData.close();
return cur;
};
//////////////////////////
public Cursor getAns(int soalid)
{
String myPath = DB_PATH + DB_NAME;
myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
Cursor cur;
cur = myData.rawQuery("select jawaban_desc from Jawaban where soal_soal_id='"+soalid+"'", null);
cur.moveToFirst();
myData.close();
return cur;
}
public Cursor getAnsList()
{
String myPath = DB_PATH + DB_NAME;
myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
Cursor cur;
cur = myData.rawQuery("select jawaban_desc from Jawaban", null);
cur.moveToFirst();
myData.close();
return cur;
}
public Cursor getCorrAns()
{
String myPath = DB_PATH + DB_NAME;
myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
Cursor cur;
cur = myData.rawQuery("select jawaban_corr from Soal", null);
cur.moveToFirst();
myData.close();
return cur;
}
My TestLevel1.xml
<TextView android:id="@+id/soal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="90dp" android:layout_marginTop="80dp" android:textAppearance="?android:attr/textAppearanceMedium" /> <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/soal" android:layout_marginTop="43dp" > <RadioButton android:id="@+id/radio0" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <RadioButton android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <RadioButton android:id="@+id/radio3" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RadioGroup> <TextView android:id="@+id/tvScore" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/radioGroup1" android:layout_below="@+id/radioGroup1" android:layout_marginTop="24dp" android:text="Score: " android:textAppearance="?android:attr/textAppearanceMedium" /> <Button android:id="@+id/nextBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/score" android:background="@drawable/lanjut" />
My TestLevel1.java
public class TestLevel1 extends Activity {
private RadioButton radioButton;
private TextView quizQuestion;
private TextView tvScore;
private int rowIndex = 1;
private static int score=0;
private int questNo=0;
private boolean checked=false;
private boolean flag=true;
private RadioGroup radioGroup;
String[] corrAns = new String[5];
final DatabaseHelper db = new DatabaseHelper(this);
Cursor c1;
Cursor c2;
Cursor c3;
int counter=1;
String label;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_level1);
String options[] = new String[19];
// get reference to radio group in layout
RadioGroup radiogroup = (RadioGroup) findViewById(R.id.radioGroup1);
// layout params to use when adding each radio button
LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
try {
db.createDatabase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
c3 = db.getCorrAns();
tvScore = (TextView) findViewById(R.id.tvScore);
for (int i=0;i<=4;i++)
{
corrAns[i]=c3.getString(0);
c3.moveToNext();
}
radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
for(int i=0; i<4;i++) {
RadioButton btn = (RadioButton) radioGroup.getChildAt(i);
String text;
if (btn.isPressed() && btn.isChecked() && questNo < 5)
{
Log.e("corrAns[questNo]",corrAns[questNo]);
if (corrAns[questNo].equals(btn.getText()) && flag==true)
{
score++;
flag=false;
checked = true;
}
else if(checked==true)
{
score--;
flag=true;
checked = false;
}
}
}
tvScore.setText("Score: " + Integer.toString(score) + "/5");
Log.e("Score:", Integer.toString(score));
}
});
quizQuestion = (TextView) findViewById(R.id.soal);
displayQuestion();
/*Displays the next options and sets listener on next button*/
Button nextBtn = (Button) findViewById(R.id.nextBtn);
nextBtn.setOnClickListener(nextBtn_Listener);
}
/*Called when next button is clicked*/
private View.OnClickListener nextBtn_Listener= new View.OnClickListener() {
@Override
public void onClick(View v) {
((View) nextBtn_Listener).setBackgroundResource(R.drawable.lanjut1);
flag=true;
checked = false;
questNo++;
if (questNo < 5)
{
c1.moveToNext();
displayQuestion();
}
}
};
private void displayQuestion()
{
//Fetching data quiz data and incrementing on each click
c1=db.getSoal_Content(rowIndex);
c2 =db.getAns(rowIndex++);
quizQuestion.setText(c1.getString(0));
radioGroup.removeAllViews();
for (int i=0;i<=3;i++)
{
//Generating and adding 4 radio buttons dynamically
radioButton = new RadioButton(this);
radioButton.setText(c2.getString(0));
radioButton.setId(i);
c2.moveToNext();
radioGroup.addView(radioButton);
}
}
}
Please help.... Thank you :)