尝试在Android中获取所有SMS消息时出现NullPointerException

时间:2014-04-22 08:58:03

标签: java android nullpointerexception

我正在尝试使用

获取发送/收件箱/草稿类型的所有短信
Uri uri= Uri.parse("content://sms/");

它会检索所有已发送/收件箱邮件,但会提供

java.lang.NullPointerException

在“地址”栏中,我也尝试使用此

阅读草稿短信
Uri uri = Uri.parse("content://sms/draft/"); 

但异常是一样的 任何人都可以帮助我如何解决这个问题 这是我的代码

private void getSMSDetails() {
    StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append("*********SMS History*************** :");
    Uri uri = Uri.parse("content://sms");
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);

    if (cursor.moveToFirst()) {
        for (int i = 0; i < cursor.getCount(); i++) {
            String body = cursor.getString(cursor.getColumnIndexOrThrow("body"))
                    .toString();
            String number = cursor.getString(cursor.getColumnIndexOrThrow("address"))
                    .toString();
            //       String name = cursor.getString(cursor.getColumnIndexOrThrow("name"))
            //             .toString();

            String date = cursor.getString(cursor.getColumnIndexOrThrow("date"))
                    .toString();
            Date smsDayTime = new Date(Long.valueOf(date));
            String type = cursor.getString(cursor.getColumnIndexOrThrow("type"))
                    .toString();
            String typeOfSMS = null;
            switch (Integer.parseInt(type)) {
                case 1:
                    typeOfSMS = "INBOX";
                    break;

                case 2:
                    typeOfSMS = "SENT";
                    break;

                case 3:
                    typeOfSMS = "DRAFT";
                    break;
            }

            Log.d("Some details", "\nPhone Number:--- " + number + " \nMessage Type:--- "
                    + typeOfSMS + " \nMessage Date:--- " + smsDayTime
                    + " \nMessage Body:--- " + body
                    + "Person name:--" + getContactName(
                    getApplicationContext(),
                    cursor.getString(cursor
                            .getColumnIndexOrThrow("address"))
            ));
            stringBuffer.append("\n----------------------------------");
            cursor.moveToNext();
        }

    }
    cursor.close();

}



String getContactName(Context context, String phoneNumber) {
        ContentResolver cr = context.getContentResolver();
        Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
                Uri.encode(phoneNumber));
        Cursor cursor = cr.query(uri,
                new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null);
        if (cursor == null) {
            return null;
        }
        String contactName = null;
        if (cursor.moveToFirst()) {
            contactName = cursor.getString(cursor
                    .getColumnIndex(PhoneLookup.DISPLAY_NAME));
        }
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
        return contactName;
    }

这里是logcat

04-22 13:45:13.406: D/AndroidRuntime(19046): Shutting down VM
04-22 13:45:13.406: W/dalvikvm(19046): threadid=1: thread exiting with uncaught exception (group=0x412d4300)
04-22 13:45:13.418: E/AndroidRuntime(19046): FATAL EXCEPTION: main
04-22 13:45:13.418: E/AndroidRuntime(19046): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.smsdata/com.example.smsdata.MainActivity}: java.lang.NullPointerException
04-22 13:45:13.418: E/AndroidRuntime(19046):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-22 13:45:13.418: E/AndroidRuntime(19046):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-22 13:45:13.418: E/AndroidRuntime(19046):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-22 13:45:13.418: E/AndroidRuntime(19046):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-22 13:45:13.418: E/AndroidRuntime(19046):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 13:45:13.418: E/AndroidRuntime(19046):    at android.os.Looper.loop(Looper.java:137)
04-22 13:45:13.418: E/AndroidRuntime(19046):    at android.app.ActivityThread.main(ActivityThread.java:4745)
04-22 13:45:13.418: E/AndroidRuntime(19046):    at java.lang.reflect.Method.invokeNative(Native Method)
04-22 13:45:13.418: E/AndroidRuntime(19046):    at java.lang.reflect.Method.invoke(Method.java:511)
04-22 13:45:13.418: E/AndroidRuntime(19046):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-22 13:45:13.418: E/AndroidRuntime(19046):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-22 13:45:13.418: E/AndroidRuntime(19046):    at dalvik.system.NativeStart.main(Native Method)
04-22 13:45:13.418: E/AndroidRuntime(19046): Caused by: java.lang.NullPointerException
04-22 13:45:13.418: E/AndroidRuntime(19046):    at com.example.smsdata.MainActivity.getSMSDetails(MainActivity.java:55)
04-22 13:45:13.418: E/AndroidRuntime(19046):    at com.example.smsdata.MainActivity.onCreate(MainActivity.java:26)
04-22 13:45:13.418: E/AndroidRuntime(19046):    at android.app.Activity.performCreate(Activity.java:5008)
04-22 13:45:13.418: E/AndroidRuntime(19046):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-22 13:45:13.418: E/AndroidRuntime(19046):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
04-22 13:45:13.418: E/AndroidRuntime(19046):    ... 11 more

1 个答案:

答案 0 :(得分:2)

使用此

获取联系人姓名
@SuppressLint("NewApi")
public static String getContactName(Context context,
        final String phoneNumber) {
    Uri uri = Uri.withAppendedPath(
            ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
            Uri.encode(phoneNumber));
    String name = "";

    ContentResolver contentResolver = context.getContentResolver();
    Cursor contactLookup = contentResolver.query(uri, new String[] {
            BaseColumns._ID, ContactsContract.PhoneLookup.DISPLAY_NAME },
            null, null, null);

    try {
        if (contactLookup != null && contactLookup.getCount() > 0) {
            contactLookup.moveToNext();
            name = contactLookup.getString(contactLookup
                    .getColumnIndex(ContactsContract.Data.DISPLAY_NAME));

        }
    } finally {
        if (contactLookup != null) {
            contactLookup.close();
        }
    }

    return name;
}



private void getSMSDetails() {
    StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append("*********SMS History*************** :");
    Uri uri = Uri.parse("content://sms");
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);

    if (cursor.moveToFirst()) {
        for (int i = 0; i < cursor.getCount(); i++) {
            String body = cursor.getString(
                    cursor.getColumnIndexOrThrow("body")).toString();
            String number = cursor.getString(
                    cursor.getColumnIndexOrThrow("address")).toString();
            // String name =
            // cursor.getString(cursor.getColumnIndexOrThrow("name"))
            // .toString();

            String date = cursor.getString(
                    cursor.getColumnIndexOrThrow("date")).toString();
            Date smsDayTime = new Date(Long.valueOf(date));
            String type = cursor.getString(
                    cursor.getColumnIndexOrThrow("type")).toString();
            String typeOfSMS = null;
            switch (Integer.parseInt(type)) {
            case 1:
                typeOfSMS = "INBOX";
                break;

            case 2:
                typeOfSMS = "SENT";
                break;

            case 3:
                typeOfSMS = "DRAFT";
                break;
            }

            Log.d("Some details","\nPhone Number:--- " + number + " \nMessage Type:--- "
                    + typeOfSMS + " \nMessage Date:--- " + smsDayTime
                    + " \nMessage Body:--- " + body
                    +"Person name:--"+ getContactName(
                            getApplicationContext(),number));
            stringBuffer.append("\n----------------------------------");
            cursor.moveToNext();
        }

    }
    cursor.close();

}