Android 在什么情况下,内容解析器返回空游标?

Android 在什么情况下,内容解析器返回空游标?,android,android-contentresolver,Android,Android Contentresolver,我使用这段代码已经很长时间了,只有很少一段时间我从内容解析器中得到null。我每次都在同一台设备上运行它,但不是每次遇到这种情况。在什么情况下,内容解析器返回空游标 static HashMap<String, String> getCallDetails(final String phoneNo,final Context context) { HashMap<String, String> dataMap = new HashMap<>()

我使用这段代码已经很长时间了,只有很少一段时间我从内容解析器中得到null。我每次都在同一台设备上运行它,但不是每次遇到这种情况。在什么情况下,内容解析器返回空游标

static HashMap<String, String> getCallDetails(final String phoneNo,final Context context) {
        HashMap<String, String> dataMap = new HashMap<>();
        Uri callUri = Uri.parse("content://call_log/calls");
        final Cursor cursor = context.getContentResolver().query(callUri, null, null, null, CallLog.Calls.DATE + " DESC");
        int duration;
        String dateTime;
        String dTime;
        int typeFlag;
        String callType = "";
        try {
            if (cursor != null) {
                String newNumber;
                while (cursor.moveToNext()) {
                    // newNumber is the most recent dialed or call received number
                    newNumber = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
                    if (newNumber.contains(phoneNo)) {
                        duration = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.DURATION));
                        dTime = cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATE));
                        typeFlag = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));
                        switch (typeFlag) {
                            case CallLog.Calls.OUTGOING_TYPE:
                                callType = "OUTGOING";
                                break;
                        case CallLog.Calls.INCOMING_TYPE:
                            callType = "INCOMING";
                            break;
                    }
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
                    dateTime = formatter.format(new Date(Long.parseLong(dTime)));
                    dataMap.put("duration", String.valueOf(duration));
                    dataMap.put("dateTime", dateTime);
                    dataMap.put("callType", callType);
                    break;
                }
            }
        } else {
            appendLog("getCallDetails : cursor : cursor value is null");
        }
    } catch (Exception e) {
        String methodName = Thread.currentThread().getStackTrace()[2].getMethodName();
        String className = Thread.currentThread().getStackTrace()[2].getClassName();
        appendLog("Exception in " + className + " : " + methodName + " : " + e);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return dataMap;
}
静态HashMap getCallDetails(最终字符串phoneNo,最终上下文){
HashMap dataMap=新的HashMap();
Uri callUri=Uri.parse(“content://call_log/calls");
最终游标Cursor=context.getContentResolver().query(callUri,null,null,null,CallLog.Calls.DATE+“DESC”);
int持续时间;
字符串日期时间;
字符串数据时间;
int-typeFlag;
字符串callType=“”;
试一试{
如果(光标!=null){
字符串newNumber;
while(cursor.moveToNext()){
//newNumber是最近拨打或收到的电话号码
newNumber=cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
if(newNumber.contains(phoneNo)){
duration=cursor.getInt(cursor.getColumnIndex(CallLog.Calls.duration));
dTime=cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATE));
typeFlag=cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));
开关(类型标志){
案例CallLog.Calls.OUTGOING_类型:
callType=“传出”;
打破
案例CallLog.Calls.INCOMING_类型:
callType=“传入”;
打破
}
SimpleDataFormat格式化程序=新的SimpleDataFormat(“yyyy-MM-dd HH:MM:ss”,Locale.getDefault());
dateTime=formatter.format(新日期(Long.parseLong(dTime));
put(“duration”,String.valueOf(duration));
dataMap.put(“dateTime”,dateTime);
dataMap.put(“callType”,callType);
打破
}
}
}否则{
appendLog(“getCallDetails:游标:游标值为空”);
}
}捕获(例外e){
String methodName=Thread.currentThread().getStackTrace()[2].getMethodName();
字符串className=Thread.currentThread().getStackTrace()[2].getClassName();
appendLog(“异常出现在“+className+”:“+methodName+”:“+e”);
}最后{
如果(光标!=null){
cursor.close();
}
}
返回数据图;
}

试试这个

Cursor cursor = context.getContentResolver().query(
            CallLog.Calls.CONTENT_URI, null, null, null,
            CallLog.Calls.DATE + " DESC");
    int number = cursor.getColumnIndex(CallLog.Calls.NUMBER);
    int type = cursor.getColumnIndex(CallLog.Calls.TYPE);
    int date = cursor.getColumnIndex(CallLog.Calls.DATE);
    int duration = cursor.getColumnIndex(CallLog.Calls.DURATION);

可能对你有用

你的问题是什么?您只是陈述了一些事实,但并没有回答任何问题。在什么情况下,内容解析器返回空游标?谢谢你的回复。我已经试过了,但在一些罕见的情况下,我也从中得到了null cursor..没有回答这个问题,因为您仍然在使用cursor来获得int,问题是有时候cursor为null,并且在访问其方法时抛出异常。