【错误记录】反射时调用方法及成员报错 ( 执行反射方法 | 设置反射的成员变量 | 设置方法/成员可见性 )

简介: 【错误记录】反射时调用方法及成员报错 ( 执行反射方法 | 设置反射的成员变量 | 设置方法/成员可见性 )

文章目录

一、报错信息

二、解决方案





一、报错信息


在执行反射方法时 , 反射方法后 , 直接调用该方法 ;


// 获取 View 的 getListenerInfo 方法
Method getListenerInfo = null;
try {
    getListenerInfo = View.class.getDeclaredMethod("getListenerInfo");
} catch (NoSuchMethodException e) {
    e.printStackTrace();
}
// 执行 View view 对象的 getListenerInfo 方法
Object mListenerInfo = null;
try {
    mListenerInfo = getListenerInfo.invoke(view);
} catch (IllegalAccessException e) {
    e.printStackTrace();
} catch (InvocationTargetException e) {
    e.printStackTrace();
}



出现如下报错信息 :


2021-06-17 10:39:08.146 3297-3297/com.example.plugin_hook W/System.err: java.lang.IllegalAccessException: Class java.lang.Class<com.example.plugin_hook.MainActivity> cannot access  method android.view.View$ListenerInfo android.view.View.getListenerInfo() of class java.lang.Class<android.view.View>
2021-06-17 10:39:08.146 3297-3297/com.example.plugin_hook W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2021-06-17 10:39:08.146 3297-3297/com.example.plugin_hook W/System.err:     at com.example.plugin_hook.MainActivity.hook(MainActivity.java:56)
2021-06-17 10:39:08.146 3297-3297/com.example.plugin_hook W/System.err:     at com.example.plugin_hook.MainActivity.onCreate(MainActivity.java:32)
2021-06-17 10:39:08.147 3297-3297/com.example.plugin_hook W/System.err:     at android.app.Activity.performCreate(Activity.java:7144)
2021-06-17 10:39:08.147 3297-3297/com.example.plugin_hook W/System.err:     at android.app.Activity.performCreate(Activity.java:7135)
2021-06-17 10:39:08.147 3297-3297/com.example.plugin_hook W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
2021-06-17 10:39:08.147 3297-3297/com.example.plugin_hook W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2931)
2021-06-17 10:39:08.147 3297-3297/com.example.plugin_hook W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3086)
2021-06-17 10:39:08.147 3297-3297/com.example.plugin_hook W/System.err:     at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
2021-06-17 10:39:08.147 3297-3297/com.example.plugin_hook W/System.err:     at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
2021-06-17 10:39:08.147 3297-3297/com.example.plugin_hook W/System.err:     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
2021-06-17 10:39:08.147 3297-3297/com.example.plugin_hook W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
2021-06-17 10:39:08.147 3297-3297/com.example.plugin_hook W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
2021-06-17 10:39:08.147 3297-3297/com.example.plugin_hook W/System.err:     at android.os.Looper.loop(Looper.java:193)
2021-06-17 10:39:08.147 3297-3297/com.example.plugin_hook W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6718)
2021-06-17 10:39:08.147 3297-3297/com.example.plugin_hook W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2021-06-17 10:39:08.147 3297-3297/com.example.plugin_hook W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
2021-06-17 10:39:08.147 3297-3297/com.example.plugin_hook W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2021-06-17 10:39:08.147 3297-3297/com.example.plugin_hook W/ple.plugin_hoo: Accessing hidden field Landroid/view/View$ListenerInfo;->mOnClickListener:Landroid/view/View$OnClickListener; (light greylist, reflection)
2021-06-17 10:39:08.149 3297-3297/com.example.plugin_hook E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.plugin_hook, PID: 3297
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.plugin_hook/com.example.plugin_hook.MainActivity}: java.lang.NullPointerException: null receiver
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2951)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3086)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6718)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.NullPointerException: null receiver
        at java.lang.reflect.Field.get(Native Method)
        at com.example.plugin_hook.MainActivity.hook(MainActivity.java:86)
        at com.example.plugin_hook.MainActivity.onCreate(MainActivity.java:32)
        at android.app.Activity.performCreate(Activity.java:7144)
        at android.app.Activity.performCreate(Activity.java:7135)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2931)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3086) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6718) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)







二、解决方案


执行所有的反射方法 , 设置成员变量 之前 , 都要设置可见性 ;


   

// 执行所有的反射方法 , 设置成员变量 之前 , 都要设置可见性
        getListenerInfo.setAccessible(true);


只要使用了反射 , 说明通过正常途径是无法运行的 , 因此凡是涉及到 反射方法执行 , 反射成员访问 , 一律设置可见性 ;



修改后代码 :


   

// 获取 View 的 getListenerInfo 方法
        Method getListenerInfo = null;
        try {
            getListenerInfo = View.class.getDeclaredMethod("getListenerInfo");
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        // 执行所有的反射方法 , 设置成员变量 之前 , 都要设置可见性
        getListenerInfo.setAccessible(true);
        // 执行 View view 对象的 getListenerInfo 方法
        Object mListenerInfo = null;
        try {
            mListenerInfo = getListenerInfo.invoke(view);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }



目录
相关文章
|
Unix API 调度
POSIX线程基本操作
POSIX线程基本操作
244 0
|
Cloud Native 容灾 安全
Nacos 开源、自研、商业化三位一体战略解读
Nacos作为整个阿里云原生三位战略中的核心组成部分,我们在2018年以Configserver/VIPServer/Diamond为基础通过Nacos开源输出阿里十年沉淀的注册中心和配置中心能力,并且快速成为国内首选。并且通过云产品MSE以BaaS模式输出解决方案能力。
1315 91
|
存储 运维 容灾
使用NineData实现亿级别MySQL大表迁移
NineData提供了高效、稳定的MySQL大表迁移能力,解决了传统迁移方案的问题。通过智能分片、行级并发和动态攒批等核心技术,NineData保证了迁移性能。同时,NineData具备完善的容灾能力,提高了大表迁移的成功率。通过数据和结构的对比功能,保障了数据的一致性。使用NineData进行迁移任务的配置简单快捷,只需一分钟即可完成。NineData还提供了丰富的检查项和观测、干预能力,帮助用户追踪迁移进展并处理异常情况。
1103 0
使用NineData实现亿级别MySQL大表迁移
|
敏捷开发 设计模式 监控
深入探究软件测试中的自动化边界
【4月更文挑战第30天】 在追求高效与质量保障的软件开发领域,自动化测试已成为一个不可或缺的环节。本文旨在探讨软件测试自动化的实践边界,通过分析自动化测试的优势与局限,为测试工程师提供决策依据。文中不仅讨论了自动化测试适用的场景、面临的挑战,还提出了增强自动化测试效果的策略建议。
|
8天前
|
机器人 API 调度
基于 DMS Dify+Notebook+Airflow 实现 Agent 的一站式开发
本文提出“DMS Dify + Notebook + Airflow”三位一体架构,解决 Dify 在代码执行与定时调度上的局限。通过 Notebook 扩展 Python 环境,Airflow实现任务调度,构建可扩展、可运维的企业级智能 Agent 系统,提升大模型应用的工程化能力。
|
14天前
|
人工智能 数据可视化 Java
Spring AI Alibaba、Dify、LangGraph 与 LangChain 综合对比分析报告
本报告对比Spring AI Alibaba、Dify、LangGraph与LangChain四大AI开发框架,涵盖架构、性能、生态及适用场景。数据截至2025年10月,基于公开资料分析,实际发展可能随技术演进调整。
913 152