I am trying to use the snippet:
GenericModel.class.getDeclaredMethod("findById");
to get a Method called "findById". I know the method exists, because when I call:
GenericModel.class.getDeclaredMethods();
the method is listed in the array returned.
However, when using the first snippet, I am getting a java.lang.NoSuchMethodException? Why?
Presumably findById
actually takes parameters. But you are searching for a method by that name that takes none. Most likely what you want is:
GenericModel.class.getDeclaredMethod("findById", new Class[] { int.class });
This will match a method that has a signature like this:
Object findById(int id) { ... }
getDeclaredMethod()
receives parameter types as well, and you didn't give it any, and in the case of findViewById
, it's a method that receives an int
as parameter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With