I have java code like this:
Method m = device.getClass()
.getMethod("removeBond", (Class[]) null);
m.invoke(device, (Object[]) null);
and i am trying to write the same thing in kotlin like this:
device.javaClass.getMethod("removeBondNative", null as Class<*>).invoke(device, null as Any)
but i get this error message:
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)
i also tried changing null to Unit:
device.javaClass.getMethod("removeBondNative", Unit as Class<*>).invoke(device, null as Any)
but still encounter error:
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)
what i am doing wrong here? how can java reflection be mirrored in kotlin in a situation like this?
Aucun commentaire:
Enregistrer un commentaire