文章目錄
- 一、報錯資訊
- 二、解決方案
一、報錯資訊
在執行反射方法時 , 反射方法後 , 直接調用該方法 ;
// 擷取 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();
}