无法使用Java的vararg参数而不是vararg参数调用函数

时间:2019-08-07 15:02:14

标签: android kotlin

在我的android项目中,我使用java和kotlin代码:

使用Kotlin代码:

fun getOperationsList(vararg typesList: OperationType, operationStatusList: List<OperationStatus>) {
...
}

以下Java代码:

List<OperationStatus> operationStatusList = new ArrayList<>();
        operationStatusList.add(OperationStatus.EXECUTED);
    TransportService.INSTANCE.getOperationsList(OperationType.PAYMENT, OperationType.PAYOUT, operationStatusList);

但是我在Java代码中遇到编译错误:

> Task :app:compileDebugJavaWithJavac
Gradle may disable incremental compilation as the following annotation processors are not incremental: jetified-androidannotations-4.6.0.jar (org.androidannotations:androidannotations:4.6.0).
Consider setting the experimental feature flag android.enableSeparateAnnotationProcessing=true in the gradle.properties file to run annotation processing in a separate task and make compilation incremental.
warning: A class activity declared in the AndroidManifest.xml cannot be found in the compile path: [com.soundcloud.android.crop.CropImageActivity]
fragment\navigation\ReportsFragment.java:122: error: method getOperationsList in class TransportService cannot be applied to given types;
        TransportService.INSTANCE.getOperationsList(OperationType.PAYMENT, OperationType.PAYOUT, operationStatusList);
                                 ^
  required: OperationType[],List<? extends OperationStatus>
  found: OperationType,OperationType,List<OperationStatus>
  reason: actual and formal argument lists differ in length
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
1 warning

> Task :app:compileDebugJavaWithJavac FAILED

FAILURE: Build failed with an exception.

2 个答案:

答案 0 :(得分:1)

似乎Kotlin使用Arrays表示可变参数。

 TransportService.INSTANCE.getOperationsList(
   new OperationType[]{ OperationType.PAYMENT, OperationType.PAYOUT }, 
   operationStatusList
 );

答案 1 :(得分:0)

我认为$filename = $_POST['filename']; // You really should validate this, // to prevent the user from reading any file on the server readfile("upload/$filename.txt"); function GetDate() { var filename = $('#datepicker').val(); $.ajax({ url: 'ajaxfile.php', type: 'post', data: { filename: filename }, success: function(data) { $("#date").html(data); } }); } 中的参数应该是最后一个,以实现所需的语义:

Kotlin