Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8188913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:04:37+00:00 2026-06-07T03:04:37+00:00

In the android app I’m developing, I am storing an entry and then I

  • 0

In the android app I’m developing, I am storing an entry and then I want to view it. I am able to store the entry (the code does not return any errors) but when I try to view the entries (i.e. all entries) I get a force close. Please help me. Here’s my code:

package com.ankita.try1;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class viewAttendance extends Activity {

LinearLayout l;
quer q;
@Override
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_attendance);

        l=(LinearLayout)findViewById(R.id.LinearLayout);

        q= new quer(this);
        q.open();
        Cursor c1;
        c1=q.fetchAllTodos();

        long co=1;

        if(c1!=null)
        {
            do
            {
                TextView t;

                Cursor c=q.fetchTodo(co);
                String name = c.getString(c.getColumnIndexOrThrow(quer.KEY_NAME));
                String present = c.getString(c.getColumnIndexOrThrow(quer.KEY_PRESENT));
                String total = c.getString(c.getColumnIndexOrThrow(quer.KEY_TOTAL));

                int a = (Integer.parseInt(present)/Integer.parseInt(total))*100;

                String A = a+"%";

                LayoutParams la=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
                t=new TextView(this);
                t.setLayoutParams(la);
                t.setText(name+"   "+A);                                
                l.addView(t);
                co++;
            }while(co<=c1.getCount());
            q.close();
        }
        else
        {
            q.close();
            Toast.makeText(this, "No Subjects Added", Toast.LENGTH_SHORT).show();
        }



         }

}

Here is my quer.java (where all functions are defined):

package com.ankita.try1;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import com.ankita.try1.database1;

public class quer 
{
    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "name";
    public static final String KEY_PRESENT = "present";
    public static final String KEY_ABSENT = "absent";
    public static final String KEY_TOTAL = "total";

    /*public static final int KEY_PRESENT=0;
    public static final int KEY_ABSENT=0;
    public static final int KEY_TOTAL=0;*/

    private static final String DATABASE_TABLE = "SUBJECT";
    private Context context;
    private SQLiteDatabase database;
    private database1 dbHelper;
    public quer(Context context) 
    {
        this.context = context;
    }

    public quer open() throws SQLException 
    {
        dbHelper = new database1(context);
        database = dbHelper.getWritableDatabase();
        return this;
    }

    public void close() 
    {
        dbHelper.close();
    }
    private ContentValues createContentValues(String name, String present, String absent,String total) 
    {
        ContentValues values = new ContentValues();
        values.put(KEY_NAME, name);
        values.put(KEY_PRESENT, present);
        values.put(KEY_ABSENT, absent);
        values.put(KEY_TOTAL, total);
        return values;
    }
    //TO CREATE A ROW
    public long createTodo(String name, String present, String absent,String total) {
        ContentValues initialValues = createContentValues(name, present,absent,total);
                return database.insert(DATABASE_TABLE, null, initialValues);
    }
    //TOI DELETE A ROW
    public boolean deleteTodo(long rowId) 
    {
        return database.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }
    //FETCH ALL ROWS
    public Cursor fetchAllTodos() 
    {
        return database.query(DATABASE_TABLE, new String[] { KEY_ROWID,KEY_NAME, KEY_PRESENT, KEY_ABSENT, KEY_TOTAL}, null, null, null,null, null);
    }
    //UPDATE THE DATABASE
    public boolean updateTodo(long rowId, String name, String present, String absent,String total) 
    {
        ContentValues updateValues = createContentValues(name, present,absent,total);
        return database.update(DATABASE_TABLE, updateValues, KEY_ROWID + "="
                + rowId, null) > 0;
    }
    //FETCH ACC. TO _ID
    public Cursor fetchTodo(long rowId) throws SQLException {
        Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {
                KEY_ROWID, KEY_NAME, KEY_PRESENT, KEY_ABSENT, KEY_TOTAL },
                KEY_ROWID + "=" + rowId, null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }
    //FETCH ACC. TO NAME
    public Cursor fetchTodo(String name) throws SQLException {
        Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {
                KEY_ROWID, KEY_NAME, KEY_PRESENT, KEY_ABSENT, KEY_TOTAL },
                KEY_NAME + "=" + name, null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }
}

I cannot view the database entries i.e. I don’t see anything of viewAttendance Activity being executed – I get a force close – where am I going wrong?

Here is the logcat:

07-05 19:06:16.304: D/AndroidRuntime(29530): Shutting down VM
07-05 19:06:16.304: W/dalvikvm(29530): threadid=1: thread exiting with uncaught exception (group=0x40018578)
07-05 19:06:16.328: E/AndroidRuntime(29530): FATAL EXCEPTION: main
07-05 19:06:16.328: E/AndroidRuntime(29530): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ankita.try1/com.ankita.try1.viewAttendance}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
07-05 19:06:16.328: E/AndroidRuntime(29530):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
07-05 19:06:16.328: E/AndroidRuntime(29530):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
07-05 19:06:16.328: E/AndroidRuntime(29530):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-05 19:06:16.328: E/AndroidRuntime(29530):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
07-05 19:06:16.328: E/AndroidRuntime(29530):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 19:06:16.328: E/AndroidRuntime(29530):    at android.os.Looper.loop(Looper.java:130)
07-05 19:06:16.328: E/AndroidRuntime(29530):    at android.app.ActivityThread.main(ActivityThread.java:3687)
07-05 19:06:16.328: E/AndroidRuntime(29530):    at java.lang.reflect.Method.invokeNative(Native Method)
07-05 19:06:16.328: E/AndroidRuntime(29530):    at java.lang.reflect.Method.invoke(Method.java:507)
07-05 19:06:16.328: E/AndroidRuntime(29530):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
07-05 19:06:16.328: E/AndroidRuntime(29530):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
07-05 19:06:16.328: E/AndroidRuntime(29530):    at dalvik.system.NativeStart.main(Native Method)
07-05 19:06:16.328: E/AndroidRuntime(29530): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
07-05 19:06:16.328: E/AndroidRuntime(29530):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
07-05 19:06:16.328: E/AndroidRuntime(29530):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
07-05 19:06:16.328: E/AndroidRuntime(29530):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
07-05 19:06:16.328: E/AndroidRuntime(29530):    at com.ankita.try1.viewAttendance.onCreate(viewAttendance.java:36)
07-05 19:06:16.328: E/AndroidRuntime(29530):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-05 19:06:16.328: E/AndroidRuntime(29530):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
07-05 19:06:16.328: E/AndroidRuntime(29530):    ... 11 more
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:04:38+00:00Added an answer on June 7, 2026 at 3:04 am

    Im moving this to answer since its a bit too long for a comment.
    First access the database within a thread, you can use AsyncTask, Threads or CursorLoaders.

    also to move through the cursor try using cursor.moveToNext() and instead of a do-while use a simple while and check if its not afterlast with:

    while(!cursor.isAfterLast){
        cursor.moveToNext();
        //do-something
    }
    

    also where you have this:

    c1=q.fetchAllTodos();
    
            long co=1;
    
            if(c1!=null)
            { //something } 
    

    you are checking that your cursor is not null but remember that your cursor can be empty and that might cause you a problem (wich i guess its the one you are having)
    so to fix it add a validation like:

    if(c1!=null && c1.getCount()!=0) 
     { //something } 
    

    also to update your View you cannot do so within a non-UI thread so you will need a handler, a post or other methods. If you will use an AsyncTask I recommend you do this in your onPostExecute.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My android app is not in the app store yet. Is it possible to
My Android app is specifically designed for tablet, and I do not want it
My android app will work for both normal and hdpi device. I don't want
My Android app has a button that executes this code in order to open
My Android app stopped working on Friday. For some reason it's not being updated
My android app was able to generate text files with key value pairs. Now,
My android app is supposed to go through twitter using twitter4j libraries, and return
The android app I am working on overrides the Application class to store lightweight
My android app that I am developing needs to request a page on my
import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.