public class EventHandler implements OnClickListener, OnItemSelectedListener,
OnItemClickListener, OnItemLongClickListener { private Object context;
private String methodName;
private Class<? extends Object> activityType;
private java.lang.reflect.Method method; public EventHandler(Object context, String methodName) {
this.context = context;
this.activityType = context.getClass();
this.methodName = methodName;
} @Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
try {
// 得到指定委托方法的类型
method = activityType.getMethod(this.methodName, new Class[] {
int.class, long.class });

// 调用指定的方法
method.invoke(this.context, new Object[] { position, id });
} catch (Exception ex) {
return;
}
}
}
上面的反射中,如果我变成method = activityType.getMethod(this.methodName, new Class[] {
View.class,int.class, long.class });

// 调用指定的方法
method.invoke(this.context, new Object[] { view,position, id });也就是把android的系统接口传过来的参数全部用上,就会出现找不到方法的错误,但实际上这个方法是存在的,在getMethod那一步就出错了。如果不使用View这个参数,就是正常的