![](/img/trans.png)
[英]This java code compiles, but is wrong. Can you tell me what is wrong with it?
[英]The findAndHookMethod code in java Xposed is wrong. Help!! (NoSuchMethodError)
挂钩目标:ButtonClickCount(int a)
如果下面的代码
public int ButtonClickCount()
{
return 1;
}
使用下面的挂钩代码成功。
findAndHookMethod(
"com.study.MainActivity",
lpparam.classLoader,
"ButtonClickCount",
new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
super.beforeHookedMethod(param);
XposedBridge.log("before");
}
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
super.afterHookedMethod(param);
XposedBridge.log("after");
param.setResult(10000);
}
}
);
}
但是,如果 ButtonClickCount 方法接收到参数值,则会引发 NoSuchMethodError。
public int ButtonClickCount(int a)
{
return a++;
}
这是 findAndHookMethod 中的错误,我不知道要在代码中添加什么。
我解决了。
示例代码 1
public int ButtonClickCount() {
return 1;
}
.
findAndHookMethod(
"com.study.MainActivity",
lpparam.classLoader,
"ButtonClickCount",
new XC_MethodHook()
.
示例代码 2
public int ButtonClickCount(int a) {
return a++;
}
.
findAndHookMethod(
"com.study.MainActivity",
lpparam.classLoader,
"ButtonClickCount",
int.class,
new XC_MethodHook()
示例代码 3
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView((int) R.layout.activity_main);
this.btn = (Button) findViewById(R.id.btn);
this.up = (TextView) findViewById(R.id.up);
this.btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
.
findAndHookMethod(
"com.study.MainActivity",
lpparam.classLoader,
"ButtonClickCount",
Bundle.class,
new XC_MethodHook()
从上面的示例代码中可以看出,如果您要挂钩的方法不带参数值,您可以简单地输入 class 名称、类加载器、方法名称、object。
但是,如果您从要挂钩的方法中收到一个 int 参数值 a,如示例代码 2 所示,则必须在 findAndHookMethod 中添加 int.class。 如果您不添加它,您将在 Xposed 应用程序中看到 NoSuchMethodError。
如果您尝试像示例代码 3 中那样挂钩 onCreate 方法,则 onCreate 方法采用 Bundle 类型的参数。 因此,您需要在 findAndHookMethod 中添加 Bundle.class。
如果这不起作用,请检查您的 Android Studio 版本。 就我而言,我从 3.6.2 版本降级到 2.3.2 版本,然后运行它。
我希望我的解决方案对某人有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.