如何解决outofboundexception?

时间:2013-04-03 12:35:14

标签: android database while-loop android-cursor

我通过一个查询访问电话簿并从其他查询中获取最后的呼叫详细信息。问题是,当我从我的manged游标获取日期时,我得到了outofbound异常,我的应用程序崩溃了。请帮助我,我只想接受每个号码的最近通话日期(如果不是空的话)。

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

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    

2 个答案:

答案 0 :(得分:1)

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

尝试以此格式读取游标中的值以避免IndexoutOfBoundsException

if(mCursor.moveToFirst())
{

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

}

答案 1 :(得分:0)

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