如何在Android中获取电话记录

时间:2014-01-23 08:39:50

标签: android nullpointerexception android-manifest calllog

我正在尝试获取呼叫日志历史记录,但我正在获取nullpointer异常...正在附加 下面的代码和错误屏幕截图。我已经在Manifest file.am中添加了权限,附加了它的屏幕截图。enter image description here

public class MainActivity extends Activity {
    TextView textView = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.action_settings);       
         getCallDetails();      

    }

    private void getCallDetails() {
        // TODO Auto-generated method stub
         StringBuffer sb = new StringBuffer();      
        Cursor  managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null,null,null,null);

        int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER); 
        int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
        int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
        int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
        sb.append("Call Log :");
        while (managedCursor.moveToNext()) 
        { String phNumber = managedCursor.getString(number);
        String callType = managedCursor.getString(type);
        String callDate = managedCursor.getString(date); 
        Date callDayTime = new Date(Long.valueOf(callDate));
        String callDuration = managedCursor.getString(duration);

         String dir = null;
        int dircode = Integer.parseInt(callType);
        switch (dircode)
        { 
        case CallLog.Calls.OUTGOING_TYPE: 
            dir = "OUTGOING";
            break;
            case CallLog.Calls.INCOMING_TYPE:
                dir = "INCOMING";
                break;
                case CallLog.Calls.MISSED_TYPE:
                    dir = "MISSED";
                    break;
                    } 
        sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + callDuration); sb.append("\n----------------------------------"); } 
        //managedCursor.close();
        textView.setText(sb);


    }

}

2 个答案:

答案 0 :(得分:1)

基于logcat,出现问题的第57行似乎是这个(57-19 = onCreate()中方法调用的38行):

textView.setText(sb);

所以,textViewnull。检查您的布局activity_main.xml实际上是否包含ID为action_settings的视图。

答案 1 :(得分:0)

private static String getCallDetails(Context context) {
    StringBuffer stringBuffer = new StringBuffer();
    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);       
    while (cursor.moveToNext()) {
        String phNumber = cursor.getString(number);
        String callType = cursor.getString(type);
        String callDate = cursor.getString(date);
        Date callDayTime = new Date(Long.valueOf(callDate));
        String callDuration = cursor.getString(duration);
        String dir = null;
        int dircode = Integer.parseInt(callType);
        switch (dircode) {
        case CallLog.Calls.OUTGOING_TYPE:
            dir = "OUTGOING";
            break;
        case CallLog.Calls.INCOMING_TYPE:
            dir = "INCOMING";
            break;

        case CallLog.Calls.MISSED_TYPE:
            dir = "MISSED";
            break;
        }
        stringBuffer.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- "
                + dir + " \nCall Date:--- " + callDayTime
                + " \nCall duration in sec :--- " + callDuration);
        stringBuffer.append("\n----------------------------------");
    }
    cursor.close();
    return stringBuffer.toString();
}