前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【错误记录】反射时调用方法及成员报错 ( 执行反射方法 | 设置反射的成员变量 | 设置方法/成员可见性 )

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

作者头像
韩曙亮
发布2023-03-29 14:12:36
8280
发布2023-03-29 14:12:36
举报
文章被收录于专栏:韩曙亮的移动开发专栏

文章目录

一、报错信息


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

代码语言:javascript
复制
// 获取 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();
}

出现如下报错信息 :

代码语言:javascript
复制
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) 

二、解决方案


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

代码语言:javascript
复制
        // 执行所有的反射方法 , 设置成员变量 之前 , 都要设置可见性
        getListenerInfo.setAccessible(true);

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

修改后代码 :

代码语言:javascript
复制
        // 获取 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();
        }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-06-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、报错信息
  • 二、解决方案
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档