2010-09-29 97 views
0

我有以下程序我得到下面的異常,Android的非法參數異常

 package com.contacts; 

     import java.util.ArrayList; 
     import android.app.ListActivity; 
    import android.content.ContentResolver; 
    import android.content.Intent; 
    import android.database.Cursor; 
    import android.net.Uri; 
    import android.os.Bundle; 
    import android.provider.ContactsContract; 
    import android.provider.Contacts.People; 
    import android.provider.Contacts.People.Phones; 
    import android.provider.ContactsContract.PhoneLookup; 
    import android.view.View; 
import android.widget.ArrayAdapter; 
    import android.widget.ListAdapter; 
    import android.widget.ListView; 

    public class PickContact extends ListActivity { 

    private ListAdapter adapter; 
    private Cursor c; 
Uri contactUri; 
String contact; 
String number; 
    ContentResolver contentResolver; 
    @Override 
     public void onCreate(Bundle icicle) 
      { 
      super.onCreate(icicle); 
      setContentView(R.layout.main); 

      ArrayList<String> names = new ArrayList<String>(); 

      Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
      try 
      { 
      while(people.moveToNext()) { 
       int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME); 
       contact = people.getString(nameFieldColumnIndex); 
       int numberFieldColumnIndex = people.getColumnIndex(PhoneLookup.NUMBER); 
       number = people.getString(numberFieldColumnIndex); 
      } 

      people.close(); 
      } 
      catch(Exception e) 
      { 
       e.printStackTrace(); 

      } 

      /* ContentResolver cr = getContentResolver(); 
      c = cr.query(ContactsContract.Contacts.CONTENT_URI, null, 
        null, null,null); 

      while(c.moveToNext()){ 
        String id = 
       c.getString(c.getColumnIndex(ContactsContract.Contacts._ID)); 
         String name = 
       c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
         String number = 
       c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 

         if(number.equals("0")) continue; 

         names.add(name); 

       }*/ 


      names.add(number+""+contact); 

       adapter = new ArrayAdapter<String> (this, 
         android.R.layout.simple_list_item_1, names); 
      setListAdapter(adapter); 
      getListView().setTextFilterEnabled(true); 

     } 

}

我得到了以下異常

09-29 17:59:18.672: WARN/System.err(302): java.lang.IllegalStateException: get field slot from row 0 col -1 failed 
09-29 17:59:18.721: WARN/System.err(302):  at android.database.CursorWindow.getString_native(Native Method) 
09-29 17:59:18.721: WARN/System.err(302):  at android.database.CursorWindow.getString(CursorWindow.java:329) 
09-29 17:59:18.731: WARN/System.err(302):  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49) 
09-29 17:59:18.731: WARN/System.err(302):  at android.database.CursorWrapper.getString(CursorWrapper.java:135) 
09-29 17:59:18.741: WARN/System.err(302):  at com.contacts.PickContact.onCreate(PickContact.java:42) 
09-29 17:59:18.741: WARN/System.err(302):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
09-29 17:59:18.741: WARN/System.err(302):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
09-29 17:59:18.764: WARN/System.err(302):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
09-29 17:59:18.764: WARN/System.err(302):  at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
09-29 17:59:18.764: WARN/System.err(302):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
09-29 17:59:18.771: WARN/System.err(302):  at android.os.Handler.dispatchMessage(Handler.java:99) 
09-29 17:59:18.792: WARN/System.err(302):  at android.os.Looper.loop(Looper.java:123) 
09-29 17:59:18.792: WARN/System.err(302):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
09-29 17:59:18.792: WARN/System.err(302):  at java.lang.reflect.Method.invokeNative(Native Method) 
09-29 17:59:18.792: WARN/System.err(302):  at java.lang.reflect.Method.invoke(Method.java:521) 
09-29 17:59:18.822: WARN/System.err(302):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
09-29 17:59:18.822: WARN/System.err(302):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
09-29 17:59:18.822: WARN/System.err(302):  at dalvik.system.NativeStart.main(Native Method) 

回答

0

行0山坳-1失敗

你真的可以有一個負列ID? 也許PhoneLookup中的某個字段的起始值爲-1,並且您忘記更改它。

+0

如何更改它我不知道請告訴我 – bindal 2010-09-29 12:50:07

+1

只需在if中放置一個if以便PhoneLookup.NUMBER和PhoneLookup.DISPLAY_NAME不是-1。 – 2010-09-29 12:53:16

+0

我可以替換,而如果或設置另一個條件如果 – bindal 2010-09-29 12:56:21