我有以下这样的对象设置,并将信息传递给Async任务:

DynamoDBManager.CoreInfo passCoreInfo = new DynamoDBManager.CoreInfo();
passCoreInfo.setId(1);
passCoreInfo.setAttr("C3P0");
passCoreInfo.setMethodCallType("Get Info");

new DynamoDBManagerTask()
        .execute(passCoreInfo);


但是,尽管作为对象传递了它,然后又以某种方式将其转换为CoreInfo [],但是返回值根据我的错误期望CoreInfo。我尝试将doInBackground和AsyncTask的返回类型更改为ArrayList,这样它会期望有一个数组,但它仍然期望在return coreInfo行使用CoreInfo而不是CoreInfo []。

private class DynamoDBManagerTask extends
            AsyncTask<DynamoDBManager.CoreInfo, Void, DynamoDBManager.CoreInfo> {
        List<DynamoDBManager.Info1> info1Result = new ArrayList<DynamoDBManager.Info1>();
        List<DynamoDBManager.Info2> info2Result = new ArrayList<DynamoDBManager.Info2>();

        protected DynamoDBManager.CoreInfo doInBackground(
                DynamoDBManager.CoreInfo... coreInfo) {

            //If coreInfo[0].getMethodCallType.equals("Get Info"),  info1Result = call DynamoDB query 1
            //Else  info2Result = call DynamoDB query 2

            return coreInfo; // Should remain untouched, same info as when passed in
        }

        @Override
        protected void onPostExecute(DynamoDBManager.CoreInfo coreInfo) {

            //If coreInfo[0].getMethodCallType.equals("Get Info"), save info1Result to SQLite table 1
            //Else save info2Result to SQLite table2
        }
    }


如何修复我的代码,以使类型对齐?而且它不会抛出Required:CoreInfo Found:CoreInfo []错误?

最佳答案

参数DynamoDBManager.CoreInfo...是varargs参数,这意味着您可以将多个CoreInfo对象作为单独的参数传递,并将它们转换为数组。您必须返回该数组的元素,例如coreInfo[0]

10-07 15:30