0

我正在使用以下代码获取收件箱中短信的联系人姓名。当我尝试以下代码时,它运行良好,直到一些扩展,但在获取一半名称后给出 NullPointerException:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_smscontacts);
    Uri uri=Uri.parse("content://sms/inbox");
    Cursor cursor=getContentResolver().query(uri, null, null, null, null);
    Cursor cur=null;

    int Count=0;
    while(cursor.moveToNext())
    {
        String address=cursor.getString(cursor.getColumnIndexOrThrow("address")).toString();
        if (address != null) 
        {
            Uri person=Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, Uri.encode(address));
            String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME };
            cur=getContentResolver().query(person, projection, null, null, null);
            if(cur.moveToNext())
            {
                Log.i("Name", cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
                Log.i("count", Count++ + "");
            }
            else
                Log.i("cursor", "Null" + address);
        }
        else
            Log.i("address", "Null");

    }
    cur.close();
    cursor.close();
    Log.i("count", Count + "");

}

LogCat 如下:

06-29 15:04:41.509: E/AndroidRuntime(28700): FATAL EXCEPTION: main
06-29 15:04:41.509: E/AndroidRuntime(28700): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lovish.smscontactsdummy/com.lovish.smscontactsdummy.SMSContactsActivity}: java.lang.NullPointerException
06-29 15:04:41.509: E/AndroidRuntime(28700):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)
06-29 15:04:41.509: E/AndroidRuntime(28700):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)
06-29 15:04:41.509: E/AndroidRuntime(28700):    at android.app.ActivityThread.access$700(ActivityThread.java:134)
06-29 15:04:41.509: E/AndroidRuntime(28700):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1217)
06-29 15:04:41.509: E/AndroidRuntime(28700):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-29 15:04:41.509: E/AndroidRuntime(28700):    at android.os.Looper.loop(Looper.java:137)
06-29 15:04:41.509: E/AndroidRuntime(28700):    at android.app.ActivityThread.main(ActivityThread.java:4856)
06-29 15:04:41.509: E/AndroidRuntime(28700):    at java.lang.reflect.Method.invokeNative(Native Method)
06-29 15:04:41.509: E/AndroidRuntime(28700):    at java.lang.reflect.Method.invoke(Method.java:511)
06-29 15:04:41.509: E/AndroidRuntime(28700):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
06-29 15:04:41.509: E/AndroidRuntime(28700):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
06-29 15:04:41.509: E/AndroidRuntime(28700):    at dalvik.system.NativeStart.main(Native Method)
06-29 15:04:41.509: E/AndroidRuntime(28700): Caused by: java.lang.NullPointerException
06-29 15:04:41.509: E/AndroidRuntime(28700):    at com.lovish.smscontactsdummy.SMSContactsActivity.onCreate(SMSContactsActivity.java:38)
06-29 15:04:41.509: E/AndroidRuntime(28700):    at android.app.Activity.performCreate(Activity.java:5047)
06-29 15:04:41.509: E/AndroidRuntime(28700):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
06-29 15:04:41.509: E/AndroidRuntime(28700):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2045)
06-29 15:04:41.509: E/AndroidRuntime(28700):    ... 11 more

请帮助我需要它的解决方案..!!

4

1 回答 1

0

while循环之后,作为一个快速修复把这个:

    if (cur != null)
        cur.close();

如有疑问,请使用您的调试器。我在不到一分钟的时间内就在 Eclipse 中诊断出了这个问题......

于 2013-06-29T17:12:51.357 回答