Question

am trying to get call log history but am getting nullpointer exception...am attaching the code and error screen shot below. I have added permission in Manifest file.am attaching the screen shot of it .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);


    }

}
Was it helpful?

Solution

Based on the logcat, row 57 where the problem occurs seems to be this (57-19=38 rows from the method call in onCreate()):

textView.setText(sb);

So, textView is null. Check that your layout activity_main.xml in fact contains a view with id action_settings.

OTHER TIPS

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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top