2

我厌倦了这个问题,实际上不知道问题出在哪里,下面是我的代码:

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

这是日志猫消息:W/CursorWrapperInner(8375): Cursor finalized without prior close()

4

2 回答 2

0

这可能是旧的,但这就是原因。

不要在这里抛出异常:

readSMS() throws IOException

而是像这样包装在一个 try catch 块中:

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
}

似乎问题是在close()可以调用之前捕获了ioexception。然后在该点抛出该方法并且永远不会调用该方法。我可能是错的,我只是快速浏览了一下。我希望这有帮助。

于 2014-02-10T17:19:22.533 回答
0

完成处理后,您应该关闭该Cursor对象。

于 2013-02-28T12:37:51.567 回答