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

文章目录





一、报错信息



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

// 获取 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();
        }
上一篇:leetcode-476. 数字的补数


下一篇:交叉编译tcpreplay