【发布时间】:2020-02-11 13:18:38
【问题描述】:
我有一个具有以下方法的类:
public static int add( int a, int b ){
return a + b;
}
我正在尝试使用
从 Unity 脚本中调用它var ajc = new AndroidJavaClass( "com.mil.rfcommunitylib.BluetoothClassic" );
int result = ajc.CallStatic<int,int>( "add", new int[] { 1, 2 } );
但我明白了
AndroidJavaException: java.lang.NoSuchMethodError 在我的日志中。
怎么了?使用不带参数的方法,因此我们可以假设我正确设置了所有内容。
【问题讨论】:
-
从我的脑海中浮现:泛型类型不是用来指定返回类型吗?不应该是简单的:
ajc.CallStatic<int>( "add", new int[] { 1, 2 } );. -
不,一个模板参数没有返回类型。这里,第一个是返回类型,第二个是参数类型。
-
那么它将与您的
add方法的签名不匹配。无返回类型:CallStatic(String methodName, object[] args),返回类型:CallStatic<returnType>(String methodName, object[] args)。 -
为什么不匹配?它是
RetType CallStatic<RetType,T>( string name, T[] args ),因此int CallStatic<int,int>( string name, int[] args );-> 应该匹配我的Java 类,不是吗? -
不,因为它不是
RetType CallStatic<RetType,T>( string name, T[] args )。但你得到了下面的答案:)