0

Google タスクをアプリケーションと同期しようとしています。このために、タスクリストを取得してリストを作成する必要があるすべてのメソッドを作成したクラスを作成しました。

これらのメソッドが機能しているかどうかをテストしたいと思います。このために、を拡張するクラスを作成しました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 をメソッドに提供する方法は?

4

3 に答える 3

2

AsyncTask引数なしで execute() を呼び出すと、execute() に欠落している引数は、 AsyncTask の doInBackground()メソッドで空の (varargs) 配列になります。

でパラメータを使用できるようにする場合はdoInBackground()、 を介していくつかのパラメータを渡す必要がありますexecute()。実行時に渡されたアイテムのリストを超えてインデックスを作成しないようにする場合は、長さをチェックせずに直接インデックスを作成するのではなく、vararg arg 配列を反復処理します。

于 2016-03-31T06:33:30.120 に答える
0

最初に、Asynctask クラスを呼び出すときに値を渡していることを確認します。ArrayIndexOutOfBoundException通過していないが、それにアクセスしたいために発生します。

そのため、そのパラメーターにアクセスする前に、まずサイズが 1 より大きいかどうかを確認してください。

于 2016-03-31T06:34:02.787 に答える
0

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".

Problem

 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.

  • I assume in your doInBackground section result is illegal index .
于 2016-03-31T06:39:13.803 に答える