2013-06-28 54 views
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 

請幫我需要它的解決方案.. !!

+0

查詢後,您可以嘗試在調用cur.moveToNext()之前檢查cur = null。 –

+0

Nopes再次出現同樣的問題...它在之前的消息中檢查cur = null之後進入,但再次調用並開始將cur返回爲空。 –

+0

也許您可以嘗試while(!cursor.isAfterLast()),然後將cursor.moveToNext ()在循環的底部 –

回答

0

while循環後,作爲速戰速決把這個:

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

有疑問時,使用調試器。我在不到一分鐘的時間內在Eclipse中診斷了這個問題...

+0

非常感謝你的工作...... :) 我確實使用了調試器......但是直到今天我才遇到過這樣的錯誤......所以我一天一天都在學習更多... :) –