W/CursorWrapperInner(8375): Cursor finalized without prior close()

17 篇文章 0 订阅
i am fed up of this issue, don't know actually where is problem, Below is my code :
private void readSMS() throws IOException {
    // TODO Auto-generated method stub
    Log.d("Read SMS","Called");
    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.parse("content://sms/inbox");
    StringBuilder smsBackup = new StringBuilder(); 
    Cursor messagesCursor = cr.query(uri, new String[] { "_id","address","body","person"}, null,null, null);
    smsBackup.append("SMS Back UP (Total Message(s)::"+messagesCursor.getCount()+") \n\n");

    String name = null;
    if(messagesCursor.getCount() > 0){
        while(messagesCursor.moveToNext()){
            name = null;
            name = getName(messagesCursor.getString(messagesCursor.getColumnIndex("address")));

            if(name==null)
                name = "Sender : " + messagesCursor.getString(messagesCursor.getColumnIndex("address"));

            smsBackup.append("Sender : "+name +"\n"+  "Message : "+messagesCursor.getString(messagesCursor.getColumnIndex("body")) + "\n\n");
        }
    }

    Log.d("InSMS Lenght","::"+smsBackup.toString().length());
}


here is log cat message : W/CursorWrapperInner(8375): Cursor finalized without prior close()



This may be old but here is the reason why.

Don't throw the exception here:

readSMS() throws IOException

instead wrap in a a try catch block like so:

try{
    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.parse("content://sms/inbox");
    StringBuilder smsBackup = new StringBuilder(); 
    Cursor messagesCursor = cr.query(uri, new String[] { "_id","address","body","person"}, null,null, null);
    smsBackup.append("SMS Back UP (Total Message(s)::"+messagesCursor.getCount()+") \n\n");

    String name = null;
    if(messagesCursor.getCount() > 0){
        while(messagesCursor.moveToNext()){
            name = null;
            name = getName(messagesCursor.getString(messagesCursor.getColumnIndex("address")));

            if(name==null)
                name = "Sender : " + messagesCursor.getString(messagesCursor.getColumnIndex("address"));

            smsBackup.append("Sender : "+name +"\n"+  "Message : "+messagesCursor.getString(messagesCursor.getColumnIndex("body")) + "\n\n");
        }
    }
 messagesCursor.close();
}catch(IOException e){
//handle here, if not log it
}finally{
//can also close here if you want, need to wrap in another try block and check for null
}

Seems like the problem is the ioexception is being caught before the close() could be called. The method is then thrown at that point and is never called. I could be wrong, I just took a quick glance at it. I hope this helps.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值