一、报错信息
在执行反射方法时 , 反射方法后 , 直接调用该方法 ;
// 获取 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(); }
总结 以上是内存溢出为你收集整理的【错误记录】反射时调用方法及成员报错 ( 执行反射方法 | 设置反射的成员变量 | 设置方法/成员可见性 )全部内容,希望文章能够帮你解决【错误记录】反射时调用方法及成员报错 ( 执行反射方法 | 设置反射的成员变量 | 设置方法/成员可见性 )所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)