java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)错误

时间:2015-08-14 16:19:56

标签: java android arraylist compiler-errors indexoutofboundsexception

有人可以帮我理解这段代码有什么问题吗? 我希望能够将与查询关联的所有联系人添加到ArrayList。 但是只使用'add'命令使循环显然覆盖它​​并再次使大小为1。但是将'add'方法与索引一起使用会导致此错误。 'IndexOutOfBoundsError'。 请。帮助我。

这是错误日志。

08-14 21:36:29.893  20439-20439/? E/Zygote﹕ MountEmulatedStorage()
08-14 21:36:29.893  20439-20439/? E/Zygote﹕ v2
08-14 21:36:29.943  20439-20439/? E/SELinux﹕ [DEBUG] get_category: variable     
seinfo: default sensitivity: NULL, cateogry: NULL
08-14 21:36:44.973  20439-20439/com.cmpe277.personalassistant E/MA﹕ 
search_intent_has_started
08-14 21:36:45.103  20439-20439/com.cmpe277.personalassistant E/DU﹕ 
startQuery has launched
08-14 21:36:45.103  20439-20439/com.cmpe277.personalassistant E/DU﹕ query =  
chemist
08-14 21:36:45.383  20439-20439/com.cmpe277.personalassistant 
E/CLoaderCallbacks﹕ Nothing is null?!
08-14 21:36:53.293  20439-20439/com.cmpe277.personalassistant E/MA﹕ 
search_intent_has_started
08-14 21:36:53.393  20439-20439/com.cmpe277.personalassistant E/DU﹕ 
startQuery has launched
08-14 21:36:53.393  20439-20439/com.cmpe277.personalassistant E/DU﹕ query =  
leela ashok
08-14 21:36:53.533  20439-20439/com.cmpe277.personalassistant 
E/CLoaderCallbacks﹕ Nothing is null?!
08-14 21:36:53.563  20439-20439/com.cmpe277.personalassistant 
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.cmpe277.personalassistant, PID: 20439
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
        at 
java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
        at java.util.ArrayList.add(ArrayList.java:147)
        at  

com.cmpe277.personalassistant.ContactablesLoaderCallbacks.onLoadFinished(ContactablesLoaderCallbacks.java:117)
        at com.cmpe277.personalassistant.ContactablesLoaderCallbacks.onLoadFinished(ContactablesLoaderCallbacks.java:22)
        at android.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:483)
        at android.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:451)
        at android.content.Loader.deliverResult(Loader.java:144)
        at android.content.CursorLoader.deliverResult(CursorLoader.java:109)
        at android.content.CursorLoader.deliverResult(CursorLoader.java:42)
        at android.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:265)
        at android.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:92)
        at android.os.AsyncTask.finish(AsyncTask.java:632)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:5832)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

这是我的代码:

package com.kishore_kumar.callbacks;

import android.app.Activity;
import android.app.LoaderManager;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds;
import android.util.Log;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * Helper class to handle all the callbacks that occur when interacting with    
loaders.  Most of the
 * interesting code in this sample app will be in this file.
 */
public class ContactablesLoaderCallbacks implements   
LoaderManager.LoaderCallbacks<Cursor> {

Context mContext;

int counter = 0;

public static final String QUERY_KEY = "query";

public static final String TAG = "CLoaderCallbacks";

public ContactablesLoaderCallbacks(Context context) {
    mContext = context;
}

@Override
public Loader<Cursor> onCreateLoader(int loaderIndex, Bundle args) {
    // Where the Contactables table excels is matching text queries,
    // not just data dumps from Contacts db.  One search term is used to query
    // display name, email address and phone number.  In this case, the 
query was extracted
    // from an incoming intent in the handleIntent() method, via the
    // intent.getStringExtra() method.

    // BEGIN_INCLUDE(uri_with_query)
    String query = args.getString(QUERY_KEY);
    Uri uri = Uri.withAppendedPath(
            CommonDataKinds.Contactables.CONTENT_FILTER_URI, query);
    // END_INCLUDE(uri_with_query)


    // BEGIN_INCLUDE(cursor_loader)
    // Easy way to limit the query to contacts with phone numbers.
    String selection =
            CommonDataKinds.Contactables.HAS_PHONE_NUMBER + " = " + 1;

    // Sort results such that rows for the same contact stay together.
    String sortBy = CommonDataKinds.Contactables.LOOKUP_KEY;

    return new CursorLoader(
            mContext,  // Context
            uri,       // URI representing the table/resource to be queried
            null,      // projection - the list of columns to return.  Null 
means "all"
            selection, // selection - Which rows to return (condition rows 
must match)
            null,      // selection args - can be provided separately and 
subbed into selection.
            sortBy);   // string specifying sort order
    // END_INCLUDE(cursor_loader)
}

@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
    TextView tv  = (TextView) 
((Activity)mContext).findViewById(R.id.sample_output);
    if(tv == null) {
        Log.e(TAG, "TextView is null?!");
    } else if (mContext == null) {
        Log.e(TAG, "Context is null?");
    } else {
        Log.e(TAG, "Nothing is null?!");
    }

    // Reset text in case of a previous query
    tv.setText(mContext.getText(R.string.intro_message) + "\n\n");

    if (cursor.getCount() == 0) {
        return;
    }

    // Pulling the relevant value from the cursor requires knowing the 
column index to pull
    // it from.
    // BEGIN_INCLUDE(get_columns)
    int phoneColumnIndex = 
cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER);
    int emailColumnIndex = 
cursor.getColumnIndex(CommonDataKinds.Email.ADDRESS);
    int nameColumnIndex =  
cursor.getColumnIndex(CommonDataKinds.Contactables.DISPLAY_NAME);
    int lookupColumnIndex = 
cursor.getColumnIndex(CommonDataKinds.Contactables.LOOKUP_KEY);
    int typeColumnIndex = 
cursor.getColumnIndex(CommonDataKinds.Contactables.MIMETYPE);
    // END_INCLUDE(get_columns)

    cursor.moveToFirst();
    // Lookup key is the easiest way to verify a row of data is for the same
    // contact as the previous row.
    String lookupKey = "";
    do {
        // BEGIN_INCLUDE(lookup_key)
        String currentLookupKey = cursor.getString(lookupColumnIndex);
        if (!lookupKey.equals(currentLookupKey)) {
            String displayName = cursor.getString(nameColumnIndex);
            tv.append(displayName + "\n");
            lookupKey = currentLookupKey;
        }
        // END_INCLUDE(lookup_key)

        // BEGIN_INCLUDE(retrieve_data)
        // The data type can be determined using the mime type column.
        String mimeType = cursor.getString(typeColumnIndex);
        if (mimeType.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
            tv.append("\tPhone Number: " + 
 cursor.getString(phoneColumnIndex) + "\n");
 //@TeneCursum this is roughly about where the error gets thrown out          
 ArrayList<String> mylist = new ArrayList<String>();
            mylist.add(counter, cursor.getString(phoneColumnIndex)); //this 
adds an element to the list.
            tv.append("size = "+mylist.size());
            counter++;
        } else if (mimeType.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) 
{
            tv.append("");
        }
        // END_INCLUDE(retrieve_data)

        // Look at DDMS to see all the columns returned by a query to 
Contactables.
        // Behold, the firehose!
        for(String column : cursor.getColumnNames()) {
            Log.d(TAG, column + column + ": " +
                    cursor.getString(cursor.getColumnIndex(column)) + "\n");
        }
    } while (cursor.moveToNext());
}

@Override
public void onLoaderReset(Loader<Cursor> cursorLoader) {
}
}

我是初学者。所以请帮助我理解。

3 个答案:

答案 0 :(得分:3)

ArrayList<String> mylist = new ArrayList<String>();放在do {构造之前。问题是你正在递增计数器,但你每次都要制作一个新的列表。第二次执行do-while时,counter = 1和数组是新的,因此它的大小为0.因此,添加myList.add(1, object)将失败。

答案 1 :(得分:3)

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

在代码中放得太晚了。 它应该在任何操作发生之前创建。

您实际所做的是每次刷新并重新创建mylist。 因此,当您在循环中第二次运行它时,mylist被重新制作,并且它内部没有任何内容。

你正在访问一个只包含1个对象的新制作的ArrayList的第一个索引,即使你正在搜索2个对象,因为count是1.(记住吗?数组从0开始。)

答案 2 :(得分:1)

您正在创建一个空数组(lenght = 0),并尝试在其中放置一个位于0的不同位置的元素。观察您每次都创建一个空数组。如果您知道有多少联系人,可以使用构造函数new ArrayList(int initialCapacity)

创建一个数组