【问题标题】:W/CursorWrapperInner(8375): Cursor finalized without prior close()W/CursorWrapperInner(8375):光标在没有事先关闭的情况下完成()
【发布时间】:2013-02-14 13:28:57
【问题描述】:

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

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

【问题讨论】:

    标签: android resolver


    【解决方案1】:

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

    这里不要抛出异常:

    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。然后在该点抛出该方法并且永远不会调用该方法。我可能是错的,我只是快速浏览了一下。我希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      您应该在使用完 Cursor 对象后关闭它。

      【讨论】:

      • 你的意思是 Log.d("InSMS Lenght","::"+smsBackup.toString().length());我已经完成了,也忘记在这里打印了,仍然无法正常工作。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-26
      • 2016-07-06
      • 1970-01-01
      • 1970-01-01
      • 2015-06-08
      相关资源
      最近更新 更多