2013-04-03 19 views
0

我正在通過一個查詢訪問電話簿並從其他查詢獲取最後一次通話詳細信息。問題是,當我從我的管理遊標中獲取日期時,我得到了outofbound異常,並且我的應用程序崩潰了。請幫助我,我只想取最近每個號碼的呼叫日期(如果不爲空)。outofboundexception如何解決?

我爲獲得電話簿細節光標查詢是: -

Cursor mCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[] {Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER}, null, null, null); 
      startManagingCursor(mCursor); 

查詢獲取特定數量的最近通話日期: -

Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI,null, selection,null, Order); 

循環,其中IAM獲得的最近通話日期從電話簿詳細信息光標取得的號碼: -

mCursor.moveToFirst(); 

      while(!mCursor.isAfterLast()) { 

       JSONObject contact = new JSONObject(); 
       JSONObject parentCont = new JSONObject(); 

       String name = mCursor.getString(mCursor.getColumnIndex(Phone.DISPLAY_NAME)); 
       String number = mCursor.getString(mCursor.getColumnIndex(Phone.NUMBER)); 
       String selection = android.provider.CallLog.Calls.NUMBER+"="+number; 
       String Order = android.provider.CallLog.Calls.DATE + " DESC"; 
          Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI,null, selection,null, Order); 
          managedCursor.moveToNext(); 
       String  callDate = managedCursor.getString(managedCursor.getColumnIndex(android.provider.CallLog.Calls.DATE)); 

       Date callDayTime = new Date(Long.valueOf(callDate)); 

       //String dateColumn = cursorcalllog.getString(cursorcalllog.getColumnIndex(CallLog.Calls.DATE)); 

       Log.d("CONTACT", "Name: " + name + "| Number: " + number+"Recently Called"+callDayTime); 

Logcat錯誤: -

04-03 05:33:43.090: E/AndroidRuntime(963): FATAL EXCEPTION: main 
04-03 05:33:43.090: E/AndroidRuntime(963): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.socialize_us/com.example.socialize_us.CustomTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.socialize_us/com.example.socialize_us.PhoneBookActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 
04-03 05:33:43.090: E/AndroidRuntime(963): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2084) 
04-03 05:33:43.090: E/AndroidRuntime(963): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2111) 
04-03 05:33:43.090: E/AndroidRuntime(963): at android.app.ActivityThread.access$600(ActivityThread.java:134) 
04-03 05:33:43.090: E/AndroidRuntime(963): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1251) 
04-03 05:33:43.090: E/AndroidRuntime(963): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-03 05:33:43.090: E/AndroidRuntime(963): at android.os.Looper.loop(Looper.java:137) 
04-03 05:33:43.090: E/AndroidRuntime(963): at android.app.ActivityThread.main(ActivityThread.java:4666) 
04-03 05:33:43.090: E/AndroidRuntime(963): at java.lang.reflect.Method.invokeNative(Native Method) 
04-03 05:33:43.090: E/AndroidRuntime(963): at java.lang.reflect.Method.invoke(Method.java:511) 
04-03 05:33:43.090: E/AndroidRuntime(963): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809) 
04-03 05:33:43.090: E/AndroidRuntime(963): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576) 
04-03 05:33:43.090: E/AndroidRuntime(963): at dalvik.system.NativeStart.main(Native Method) 
04-03 05:33:43.090: E/AndroidRuntime(963): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.socialize_us/com.example.socialize_us.PhoneBookActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 
04-03 05:33:43.090: E/AndroidRuntime(963): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2084) 
04-03 05:33:43.090: E/AndroidRuntime(963): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1924) 
04-03 05:33:43.090: E/AndroidRuntime(963): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135) 
04-03 05:33:43.090: E/AndroidRuntime(963): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347) 
04-03 05:33:43.090: E/AndroidRuntime(963): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:682) 
04-03 05:33:43.090: E/AndroidRuntime(963): at android.widget.TabHost.setCurrentTab(TabHost.java:346) 
04-03 05:33:43.090: E/AndroidRuntime(963): at android.widget.TabHost.addTab(TabHost.java:236) 
04-03 05:33:43.090: E/AndroidRuntime(963): at com.example.socialize_us.CustomTabActivity.onCreate(CustomTabActivity.java:33) 
04-03 05:33:43.090: E/AndroidRuntime(963): at android.app.Activity.performCreate(Activity.java:4510) 
04-03 05:33:43.090: E/AndroidRuntime(963): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050) 
04-03 05:33:43.090: E/AndroidRuntime(963): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2048) 
04-03 05:33:43.090: E/AndroidRuntime(963): ... 11 more 
04-03 05:33:43.090: E/AndroidRuntime(963): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 
04-03 05:33:43.090: E/AndroidRuntime(963): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:400) 
04-03 05:33:43.090: E/AndroidRuntime(963): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) 
04-03 05:33:43.090: E/AndroidRuntime(963): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50) 
04-03 05:33:43.090: E/AndroidRuntime(963): at android.database.CursorWrapper.getString(CursorWrapper.java:241) 
04-03 05:33:43.090: E/AndroidRuntime(963): at com.example.socialize_us.PhoneBookActivity.onCreate(PhoneBookActivity.java:107) 
04-03 05:33:43.090: E/AndroidRuntime(963): at android.app.Activity.performCreate(Activity.java:4510) 
04-03 05:33:43.090: E/AndroidRuntime(963): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050) 
04-03 05:33:43.090: E/AndroidRuntime(963): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2048) 
04-03 05:33:43.090: E/AndroidRuntime(963): ... 21 more  
+1

您沒有使用您的查詢獲取遊標中的數據。 「索引0請求,大小爲0」。 –

+0

沒有值進入遊標,其空值 –

+0

@Arju遊標是空的,它不爲空 – Pragnani

回答

1

您的光標是空的當您試圖從mcusor中獲取值爲空的值時,您將獲取光標IndexOutOfBoundsException。

嘗試讀取這個格式從光標的值,以避免IndexOutOfBoundsException異常

if(mCursor.moveToFirst()) 
{ 

    do 
{ 
... 
... 
} 
while(mCursor.moveToNext()) 

} 
+0

當我在循環中使用託管遊標時,mcursor不是空的我得到這個異常 – Saad

+0

@Saad遊標已經使用過,如果它沒有清空它的確定,但是如果它是空的,它會爲你拋出同樣的異常。所以如果可以在光標中讀取數據。如上所述格式化您的代碼 – Pragnani

0

錯誤:android.database.CursorIndexOutOfBoundsException:致指數0請求,大小爲0 你如果等於0的值不存在任何信息,則必須添加該異常。