如何在android中修复ArrayIndexOutOfBoundsException

时间:2014-03-24 09:26:32

标签: java android

public class ReadRecords extends ListActivity {

    private String[] colummns = new String[] { ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.HAS_PHONE_NUMBER };
    private Uri contactsListUri = ContactsContract.Contacts.CONTENT_URI;
    private Uri phoneUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    private String contactIdUri = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
    private String numberUri = ContactsContract.CommonDataKinds.Phone.NUMBER;
    private Cursor cursor;
    private ArrayList<String> arrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        arrayList = new ArrayList<String>();

        cursor = managedQuery(contactsListUri, null, null, null, null);

        readContacts();

    }

    void readContacts() {

        if (cursor == null) {
            Toast.makeText(getApplicationContext(),
                    "No Contacts in Your Directory", Toast.LENGTH_SHORT).show();
        } else {
            while (cursor.moveToNext()) {
                String id = cursor
                        .getString(cursor.getColumnIndex(colummns[0]));
                String name = cursor.getString(cursor
                        .getColumnIndex(colummns[1]));
                if (Integer.parseInt(cursor.getString(cursor
                        .getColumnIndex(colummns[2]))) > 0) {

                    Cursor numCursor = managedQuery(phoneUri, null,
                            contactIdUri + "=?", new String[] { id }, null);
                    if (numCursor.getCount() > 0)
                        while (numCursor.moveToNext()) {
                            String phone = numCursor.getString(numCursor
                                    .getColumnIndex(numberUri));
                            arrayList.add(name + "~" + phone);
                        }
                }
            }

            getListView().setAdapter(
                    new MyAdapter(arrayList, R.layout.namenumber,
                            getLayoutInflater()));
        }

    }

}

这是我的代码,用于从设备检索联系人详细信息,当我向下滚动以查看联系人时,我能够查看我们的联系人,然后它显示Exception数组索引超出了Bound Expectation.length = 1; index = 1。 这个错误即将到来我无法理解我将如何修复它请建议我。

这是Log:

03-23 14:47:42.971: E/AndroidRuntime(24157): FATAL EXCEPTION: main
03-23 14:47:42.971: E/AndroidRuntime(24157): java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
03-23 14:47:42.971: E/AndroidRuntime(24157):    at com.stallion.contentproviderdemo.MyAdapter.getView(MyAdapter.java:47)
03-23 14:47:42.971: E/AndroidRuntime(24157):    at android.widget.AbsListView.obtainView(AbsListView.java:2012)
03-23 14:47:42.971: E/AndroidRuntime(24157):    at android.widget.ListView.makeAndAddView(ListView.java:1772)
03-23 14:47:42.971: E/AndroidRuntime(24157):    at android.widget.ListView.fillDown(ListView.java:672)
03-23 14:47:42.971: E/AndroidRuntime(24157):    at android.widget.ListView.fillGap(ListView.java:636)
03-23 14:47:42.971: E/AndroidRuntime(24157):    at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4546)
03-23 14:47:42.971: E/AndroidRuntime(24157):    at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:3813)
03-23 14:47:42.971: E/AndroidRuntime(24157):    at android.os.Handler.handleCallback(Handler.java:605)
03-23 14:47:42.971: E/AndroidRuntime(24157):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-23 14:47:42.971: E/AndroidRuntime(24157):    at android.os.Looper.loop(Looper.java:137)
03-23 14:47:42.971: E/AndroidRuntime(24157):    at android.app.ActivityThread.main(ActivityThread.java:4482)
03-23 14:47:42.971: E/AndroidRuntime(24157):    at java.lang.reflect.Method.invokeNative(Native Method)
03-23 14:47:42.971: E/AndroidRuntime(24157):    at java.lang.reflect.Method.invoke(Method.java:511)
03-23 14:47:42.971: E/AndroidRuntime(24157):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
03-23 14:47:42.971: E/AndroidRuntime(24157):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
03-23 14:47:42.971: E/AndroidRuntime(24157):    at dalvik.system.NativeStart.main(Native Method)


public class MyAdapter extends BaseAdapter{

    private ArrayList<String> arrayList;
    private int layoutnumber;
    private LayoutInflater inflater;

    public MyAdapter(ArrayList<String> arrayList, int namenumber, LayoutInflater inflater) {
        this.arrayList = arrayList;
        this.layoutnumber = namenumber;
        this.inflater = inflater;
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int arg0) {

        return arrayList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {

        View view = inflater.inflate(layoutnumber, arg2, false);
        TextView name = (TextView)view.findViewById(R.id.listName);
        TextView number = (TextView)view.findViewById(R.id.listNumber);
        String s[] = arrayList.get(arg0).split("~");
        name.setText(s[0]);
        number.setText(s[1]);
        return view;
    }

这是mY适配器类plz检查并告诉我哪里出错了?

2 个答案:

答案 0 :(得分:1)

这样做:

在致电此while (cursor.moveToNext()) {}之前,请致电

cursor.moveToFirst()然后尝试

答案 1 :(得分:1)

您的getView()访问数组元素存在问题。字符串split()不返回空字符串(the limit is 0),这就是为什么你可以获得长度为1的数组以及尝试访问那里的第二个元素的异常的原因。

不要在单个字符串中对数据进行编码,而是考虑使用例如而是Pair<String, String>