0

我正在通过一个查询访问电话簿并从其他查询获取最后一次通话详细信息。问题是当我从管理的光标中获取日期时,我得到了出站异常并且我的应用程序崩溃了。请帮助我,我只想获取每个号码的最近通话日期(如果不为空)。

我用于获取电话簿详细信息的光标查询是:-

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

循环,我正在获取从电话簿详细信息光标中获取的号码的最近通话日期:-

 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    
4

2 回答 2

1

您的光标为空,当您尝试从为空的 mcusor 中获取值时,您将获得光标 IndexOutOfBoundsException。

尝试以这种格式从光标中读取值以避免 IndexoutOfBoundsException

if(mCursor.moveToFirst())
{

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

}
于 2013-04-03T12:43:36.297 回答
0

错误: 引起:android.database.CursorIndexOutOfBoundsException:请求索引 0,大小为 0 如果值等于 0,则必须添加异常没有信息。

于 2013-04-03T12:44:26.377 回答