ArrayIndexOutOfBound 异常

ArrayIndexOutOfBound exception

我正在尝试将 google 任务与我的应用程序同步。为此,我创建了一个 class,其中我创建了所有需要获取任务列表和创建列表等的方法。

现在我想测试一下这些方法是否有效。为此,我创建了一个 class 扩展了 AsyncTask.

我必须在此任务中传递一些参数,因为在我有参数的方法中。就像在 getTaskList(Strig listId) 中一样。要从任何列表中获取任务,我需要提供列表的 ID。

现在,当我尝试在 AsyncTask 中传递参数时。在

中调用它时
result = gTaskSyncer.getTaskList(params[0].toString());

它在这里抛出一个 ArrayIndexOutOfBoundException

TestAsyncTask

 public class TestAsyncTask extends AsyncTask<Object,Void,List<Task>>{

    private com.google.api.services.tasks.Tasks mService = null;
    private Exception mLastError = null;
    private MainActivity activity;
    private com.google.api.services.tasks.Tasks client = null;

    public TestAsyncTask(GoogleAccountCredential credential, MainActivity activity) {
        HttpTransport transport = AndroidHttp.newCompatibleTransport();
        JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
        mService = new com.google.api.services.tasks.Tasks.Builder(
                transport, jsonFactory, credential)
                .setApplicationName("Google Tasks API Android Quickstart")
                .build();
        this.activity = activity;
    }

    protected List<Task> doInBackground(Object... params) {



        GTaskSyncer gTaskSyncer = new GTaskSyncer(activity);

        List<Task> result = new ArrayList<>();

        try {

            result = gTaskSyncer.getTaskList(params[0].toString());

        } catch (IOException e) {

            Handler handler = new Handler(Looper.getMainLooper());
            handler.post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(activity, "No tasks.", Toast.LENGTH_SHORT).show();
                }
            });

        }
        return result;
    }


    @Override
    protected void onPostExecute(List<Task> output) {
        activity.mProgress.hide();
        if (output == null || output.size() == 0) {
            activity.mOutputText.setText("No results returned.");
        } else {
            activity.mOutputText.setText(TextUtils.join("\n", output));
        }
    }
}

GTaskSyncer

 public class GTaskSyncer
{

    final MainActivity activity;
    final com.google.api.services.tasks.Tasks mService;
    private Exception mLastError = null;



    GTaskSyncer(MainActivity activity) {
        this.activity = activity;
        mService = activity.mService;
    }

    public List<TaskList> getAllTaskList() throws IOException
    {
        List<TaskList> result = new ArrayList<TaskList>();

        TaskLists taskLists = mService.tasklists().list().execute();

        for (TaskList taskList : taskLists.getItems()) {

                result.add(taskList);
        }

        return result;

    }


    public TaskList createList() throws IOException
    {

        TaskList taskList = new TaskList();

        taskList =  activity.mService.tasklists().insert(taskList).execute();

        return taskList;
    }

    public Task createTask(String listId) throws IOException
    {

        Task task = new Task();

        task =   activity.mService.tasks().insert(listId, task).execute();

        return  task;
    }

    public Task getTask(String listId,String taskId) throws IOException
    {

        Task task =   activity.mService.tasks().get(listId, taskId).execute();

        return task;
    }

    public List<Task> getTaskList(String listId) throws IOException {


        List<Task> result = new ArrayList<Task>();

        List<Task> tasks = mService.tasks().list(listId).execute().getItems();

        if (tasks != null) {

            for (Task task : tasks) {

                result.add(task);
            }
        } else {

            Toast.makeText(activity, "No tasks.", Toast.LENGTH_SHORT).show();
        }

        return result;

    }

}

我该如何测试呢?如何在调用方法时提供一个listId给方法?

当您在 AsyncTask 上调用不带任何参数的 execute() 时,execute() 缺少的参数在 AsyncTask 的 doInBackground()方法。

如果你想在doInBackground()中使用参数,那么你必须通过execute()传递一些参数。如果你想确保你没有索引超过执行中传递的项目列表,请迭代 vararg arg 数组而不是直接索引到它而不检查它的长度。

首先检查您在调用 Asynctask class 时传递了一个值。 ArrayIndexOutOfBoundException 发生是因为您没有通过但想要访问它。

因此,在访问该参数之前,首先检查大小是否大于 1。

An array out of bounds exception is a Java exception thrown due to the fact that the program is trying to access an element at a position that is outside an array limit, hence the words "Out of bounds".

问题

 result = gTaskSyncer.getTaskList(params[0].toString()); // In here Problem 

Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.

  • 我假设在您的 doInBackground 部分 result 是非法索引。