我在网上看到一些教程,它们调用简单的方法,我只需要调用接受 Context 作为参数的方法“startDownload”。我现在称它为:
Class<?> loaded = cl.loadClass("com.test.someclass");
Method m = loaded.getDeclaredMethod("startDownload", null);
m.invoke(this, null);
其中 c1 是 DexClassLoader。但没有成功。我收到 NoSuchMethodException 错误,我知道我必须在某处添加参数,但不知道在哪里...有什么建议吗?
谢谢
最佳答案
我建议查看 that post .
当调用 Class.getMethod(name, ...)
时,参数在方法名之后传递,如 here 所述.您可以直接使用您必须传递的 Class
的 class
成员:
Method myMethod = myClass.getMethod("doSomethingWithAString", String.class);
也许您忘记了其中的一些:如果签名(因此参数)不正确,将找不到该方法。
关于java - DexClassLoader - 调用带参数的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7416846/