Search code examples
androidnullpointerexceptionandroid-manifestcalllog

how to get phone call history in Android


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


    }

}

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.