强制关闭查看数据库条目:Android

时间:2012-07-05 09:08:07

标签: android database sqlite

在我正在开发的Android应用程序中,我正在存储一个条目,然后我想查看它。我能够存储条目(代码不会返回任何错误)但是当我尝试查看条目(即所有条目)时,我会收到一个强制关闭。请帮我。这是我的代码:

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();
        }



         }

}

这是我的quer.java(定义了所有函数):

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;
    }
}

我无法查看数据库条目,即我没有看到正在执行的viewAttendance活动的任何内容 - 我强行关闭 - 我哪里出错?

这是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 个答案:

答案 0 :(得分:2)

我想回答这个问题,因为它的评论太长了。 首先在线程中访问数据库,您可以使用AsyncTaskThreadsCursorLoaders

也可以通过光标移动尝试使用cursor.moveToNext()而不是使用do-while使用简单的while并检查它是否不是afterlast:

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

你也有这个:

c1=q.fetchAllTodos();

        long co=1;

        if(c1!=null)
        { //something } 

您正在检查光标是否为空,但请记住您的光标可能为空,这可能会导致您出现问题(我猜它是您的光标) 所以要修复它,添加如下的验证:

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

还要在非UI线程中更新你不能这样做的View,这样你就需要一个处理程序,一个帖子或其他方法。如果您使用AsyncTask,我建议您在onPostExecute

中执行此操作