接受空类数组的java方法的kotlin反射

接受空类数组的java方法的kotlin反射,java,android,reflection,kotlin,Java,Android,Reflection,Kotlin,我有如下java代码: Method m = device.getClass() .getMethod("removeBondNative", (Class[]) null); m.invoke(device, (Object[]) null); 我试着用Kotlin写同样的东西,就像这样: device.javaClass.getMethod("removeBondNative", null as Class<*>).invoke(device, null as

我有如下java代码:

Method m = device.getClass()
        .getMethod("removeBondNative", (Class[]) null);
m.invoke(device, (Object[]) null);
我试着用Kotlin写同样的东西,就像这样:

device.javaClass.getMethod("removeBondNative", null as Class<*>).invoke(device, null as Any)

我做错了什么?在这样的情况下,如何在Kotlin中镜像java反射?

错误消息告诉您问题:
类是非null类型,因此强制转换总是检查强制转换的值是否为null。您可以将
null作为类编写?
,但这相当于
(Class)null
。您想将
null作为数组吗?


但是,无论如何,这似乎毫无意义:因此
.getMethod(“removeBondNative”)
应该会给出相同的结果。

错误消息告诉您问题:
类是非空类型,因此强制转换总是检查强制转换的值是否为空。您可以将
null作为类编写?
,但这相当于
(Class)null
。您想将
null作为数组吗?


但是,无论如何,这似乎毫无意义:因此
.getMethod(“removeBondNative”)
应该会给出相同的结果。

removeBondNative的参数类型是什么?通常您会在那里给出参数的类,例如
Int::class.java
。为什么在这里将null强制转换为类?@Phoca参数类型可能是布尔型的,但我找到的示例都是使用null类数组实现的。我看到过一些帖子声称这对他们有效,但对我却完全无效。我将尝试更改为Boolean,但是对于调用调用,我如何知道作为参数传递什么?如果有帮助,我正在尝试解除蓝牙设备的连接。
removeBondNative
的参数类型是什么?通常您会在那里给出参数的类,例如
Int::class.java
。为什么在这里将null强制转换为类?@Phoca参数类型可能是布尔型的,但我找到的示例都是使用null类数组实现的。我看到过一些帖子声称这对他们有效,但对我却完全无效。我将尝试更改为Boolean,但是对于调用调用,我如何知道作为参数传递什么?如果有帮助的话,我正在尝试解除蓝牙设备的绑定。最终成功的是使用removeBond,如:
device::Class.java.getMethod(“removeBond”)。invoke(device)
完全忽略参数列表。最终成功的是使用removeBond,如:
device::Class.java.getMethod(“removeBond”).invoke(设备)
完全省略参数列表。
Process: com.example.zemcd.toofxchange, PID: 17466
   kotlin.TypeCastException: null cannot be cast to non-null type java.lang.Class<*>
       at com.example.zemcd.toofxchange.BluetoothUtils$Companion.unPair(BluetoothUtils.kt:61)
       at com.example.zemcd.toofxchange.DeviceAdapter$DeviceHolder$bindItems$1$$special$$inlined$forEach$lambda$1.onClick(DeviceAdapter.kt:98)
       at android.view.View.performClick(View.java:5217)
       at android.view.View$PerformClick.run(View.java:21349)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:5585)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
device.javaClass.getMethod("removeBondNative", Unit as Class<*>).invoke(device, null as Any)
Process: com.example.zemcd.toofxchange, PID: 19219
   java.lang.ClassCastException: kotlin.Unit cannot be cast to java.lang.Class
       at com.example.zemcd.toofxchange.BluetoothUtils$Companion.unPair(BluetoothUtils.kt:61)
       at com.example.zemcd.toofxchange.DeviceAdapter$DeviceHolder$bindItems$1$$special$$inlined$forEach$lambda$1.onClick(DeviceAdapter.kt:98)
       at android.view.View.performClick(View.java:5217)
       at android.view.View$PerformClick.run(View.java:21349)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:5585)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)