2011-10-17 5 views
0

연락처 번호에 액세스하려고 할 때마다 예외 범위를 벗어나는 인덱스를 얻을 때마다 누구나 내가 잘못하고있는 것을 말해 줄 수 있습니까? 클래스의안드로이드 연락처 데이터베이스에서 예외가 발생했습니다

회원들 내에서 onCreate

ContentResolver cr = getContentResolver(); 
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
    if (cur.getCount() > 0) 
    { 
     names = new String[cur.getCount()]; 
     number = new String[cur.getCount()]; 
     while (cur.moveToNext()) 
     { 
      String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); 
      String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
      names[cur.getPosition()] = name; 
      // number[cur.getPosition()] = name; 
      if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
       Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null); 
       try{ 
        number[cur.getPosition()] = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
       } 
       catch(Exception e) 
       { 
        Log.d("Exception", e.toString()); 
       } 
       phones.close(); 
      } 
     } 
    } 

스택 추적에서

private String[] names; 
private String[] number; 

/AndroidRuntime(10284): FATAL EXCEPTION: main 
E/AndroidRuntime(10284): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.informationkinetics.SMSS/com.informationkinetics.SMSS.Contact}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1 
E/AndroidRuntime(10284): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2781) 
E/AndroidRuntime(10284): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2621) 
E/AndroidRuntime(10284): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127) 
E/AndroidRuntime(10284): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339) 
E/AndroidRuntime(10284): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651) 
E/AndroidRuntime(10284): at android.widget.TabHost.setCurrentTab(TabHost.java:323) 
E/AndroidRuntime(10284): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129) 
E/AndroidRuntime(10284): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453) 
E/AndroidRuntime(10284): at android.view.View.performClick(View.java:2408) 
E/AndroidRuntime(10284): at android.view.View$PerformClick.run(View.java:8817) 
E/AndroidRuntime(10284): at android.os.Handler.handleCallback(Handler.java:587) 
E/AndroidRuntime(10284): at android.os.Handler.dispatchMessage(Handler.java:92) 
E/AndroidRuntime(10284): at android.os.Looper.loop(Looper.java:143) 
E/AndroidRuntime(10284): at android.app.ActivityThread.main(ActivityThread.java:4914) 
E/AndroidRuntime(10284): at java.lang.reflect.Method.invokeNative(Native Method) 
E/AndroidRuntime(10284): at java.lang.reflect.Method.invoke(Method.java:521) 
E/AndroidRuntime(10284): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 
E/AndroidRuntime(10284): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
E/AndroidRuntime(10284): at dalvik.system.NativeStart.main(Native Method) 
E/AndroidRuntime(10284): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1 
E/AndroidRuntime(10284): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580) 
E/AndroidRuntime(10284): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214) 
E/AndroidRuntime(10284): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41) 
E/AndroidRuntime(10284): at android.database.CursorWrapper.getString(CursorWrapper.java:140) 
E/AndroidRuntime(10284): at com.informationkinetics.SMSS.Contact.onCreate(Contact.java:44) 
E/AndroidRuntime(10284): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1065) 
E/AndroidRuntime(10284): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2745) 
E/AndroidRuntime(10284): ... 18 more 
+0

더 명확하게 스택 추적을 게시하십시오. –

+0

당신의 logcat을 게시하십시오. 문제가 무엇인지 명확하게 알려주세요. –

답변

1

시도

cur.moveToFirst(); 
추가가 phones 커서와 동일한 작업을 수행 : 당신의 while 루프 및 while(){} 편집

do{} while()에 변경하기 전에

는 시도 {} 문 앞에

phone.moveToFirst(); 

를 추가합니다.

+0

고마워, 내가 실제로 그런 바보 같은 실수를 저질렀다. – Armand

+0

도움이된다는 소식 =) – Vladimir

관련 문제