【发布时间】:2012-04-06 08:21:58
【问题描述】:
我有 Button,我可以使用它的任何方法,例如 getText、setText ...
Method mth = myButton.getClass().getMethod("getText", parameterTypes);
但是当我尝试使用相同的代码获取“setOnClickListener”方法时
Method mth = myButton.getClass().getMethod("setOnClickListener", parameterTypes); 我收到异常:“NoSuchMethodException”异常。
我尝试过的:
发送空参数
Class<?>[] parameterTypes = new Class[] {};
`Method mth = myButton.getClass().getMethod("setOnClickListener", parameterTypes);` NOT working the same Exception: "NoSuchMethodException"
i tried to get all the methods and identify it by name.
Method[] arrMethods = objElem.getClass().getMethods();
Method listener = getMethodByName(arrMethods,"setOnClickListener");
public Method getMethodByName(Method[] arrMethods,String MethodName)
{
for(int i=0;i<arrMethods.length;i++)
{
if(arrMethods[i].getName() == MethodName)
{
return arrMethods[i];
}
}
return null;
}
函数实际上没有找到。 很明显我在这里有一些误解。 可能无法达到这种方法? 提前致谢。
【问题讨论】:
标签: android reflection getmethod