在android中读取空表时应用程序崩溃 [英] Application crashes while reading an empty table in android

查看:94
本文介绍了在android中读取空表时应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题仅在于表中的内容.如果表不为空(具有一个或多个记录),则应用程序将正常运行.我正在删除表的内容,并且在读取同一张表之后,它立即引发异常并且应用程序强制关闭.

The issue is purely with the contents inside the tables. If the table is not empty (with one or more records), application works perfectly. I am deleting the contents of table and immediately after that reading the same table, it throws exception and app force closes.

我尝试搜索它,但无法得出结论.关键是:我想读取表时,在游标的movetofirst()方法上抛出的索引超出范围的异常,我想...请帮助.

I tried searching for it but couldn't conclude. The key point is : index out of bound exception which is thrown at movetofirst() method of cursor when i am going to read the table, i suppose... Please help.

public List<TableData> readForPaymentDetais()
    {
        List<TableData> paymentDetails = new ArrayList<TableData>();
        try
        {
            String selectQuery = "select * from PaymentDetails";
            SQLiteDatabase database = this.getWritableDatabase();
            Cursor cursor = database.rawQuery(selectQuery, null);
            if(cursor.getCount() !=0)
            {
                if(cursor.moveToFirst())
                {
                    do
                    {
                        TableData data = new TableData();
                        data.setPaymentMade(Float.valueOf(cursor.getString(0).toString()));
                        data.setDateOfPayment(cursor.getString(1));

                        paymentDetails.add(data);               
                    }
                    while(cursor.moveToNext());
                }               
            }

            return paymentDetails;
        }
        catch(Exception exc)
        {
            return null;
        }       
    }

推荐答案

在执行游标的moveToFirst方法之前,请检查游标是否为空.为此,您可以使用如下代码:

Before executing moveToFirst method of cursor please check whether cursor is empty. For that you can use code like:

if (mCursor.getCount() == 0) {
    // cursor is empty
}

如果光标不为空,则将您的内容放到其他部分.

If cursor is not empty put your stuff in else part.

这篇关于在android中读取空表时应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆