0

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


    }

}
Sandeep R
  • 2,284
  • 3
  • 25
  • 51
arunk
  • 185
  • 2
  • 4
  • 14

2 Answers2

1

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.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • hi am getting call log details from content Provider it's working fine but output show duplicated call details also how to avoid duplicates in call logs. current output like id name number time 1 abc 1233 2.30 pm 2 bdc 2897 1.pm 3 abc 1233 11.30 am 4 abc 1233 11.00 am but i don't want this output i need like id name number time 1 abc (3) 1233 2.30 pm 2 bdc 2897 1.00 pm – Narendra Kumar Nov 04 '14 at 06:47
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();
}
Dakshesh Khatri
  • 639
  • 7
  • 12