java.lang.NullPointerException: null receiver

最近在使用反射上遇到的错误,当反射调用一个kotlin声明的object类时,然后再反射直接调用某个方法

val targetClass = loadDexClass("xx")
val method = targetClass?.getDeclaredMethod("init", Context::class.java, DexClassLoader::class.java, String::class.java, Boolean::class.java)
method?.isAccessible = true
method?.invoke(null, context.applicationContext, mDexClassLoader, mDexPath, true)

就会报java.lang.NullPointerException: null receiver,因为kotlin的object是单例模式,所以不先实例化是会报错的

正确修改:

val targetClass = loadDexClass("xx")
val constructor = targetClass?.getDeclaredConstructor()
constructor?.isAccessible = true
val targetActivity = constructor?.newInstance()
val method = targetClass?.getDeclaredMethod("init", Context::class.java, DexClassLoader::class.java, String::class.java, Boolean::class.java)
method?.isAccessible = true
method?.invoke(targetActivity, context.applicationContext, mDexClassLoader, mDexPath, true)
06-08 10:58:36.121 1815 1815 E AndroidRuntime: Process: com.android.settings, PID: 1815 06-08 10:58:36.121 1815 1815 E AndroidRuntime: java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.supplicant.STATE_CHANGE flg=0x10 (has extras) } in com.android.settings.m8settings.receiver.WifiReceiver@41c8a5c 06-08 10:58:36.121 1815 1815 E AndroidRuntime: at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$android-app-LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1830) 06-08 10:58:36.121 1815 1815 E AndroidRuntime: at android.app.LoadedApk$ReceiverDispatcher$Args$$ExternalSyntheticLambda0.run(Unknown Source:2) 06-08 10:58:36.121 1815 1815 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:942) 06-08 10:58:36.121 1815 1815 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99) 06-08 10:58:36.121 1815 1815 E AndroidRuntime: at android.os.Looper.loopOnce(Looper.java:201) 06-08 10:58:36.121 1815 1815 E AndroidRuntime: at android.os.Looper.loop(Looper.java:288) 06-08 10:58:36.121 1815 1815 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:8061) 06-08 10:58:36.121 1815 1815 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 06-08 10:58:36.121 1815 1815 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:703) 06-08 10:58:36.121 1815 1815 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911) 06-08 10:58:36.121 1815 1815 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.wifi.SupplicantState.name()' on a null object reference 06-08 10:58:36.121 1815 1815 E AndroidRuntime: at com.android.settings.m8settings.receiver.WifiReceiver.onReceive(WifiReceiver.java:46) 06-08 10:58:36.121 1815 1815 E AndroidRuntime: at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$android-app-LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1820) 06-08 10:58:36.121 1815 1815 E AndroidRuntime: ... 9 more
06-09
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值