Android 在片段中调用公共方法

Android 在片段中调用公共方法,android,Android,在我的Android应用程序中,一个片段使用了我编写的库。我需要做的是使用反射让库中的方法访问片段中的方法。我的代码如下所示: Context c = getContext(); Class[] paramTypes = new Class[2]; paramTypes[0] = int.class; paramTypes[1] = boolean.class; java.lang.reflect.Method method = c.getClass().getMethod("someMet

在我的Android应用程序中,一个片段使用了我编写的库。我需要做的是使用反射让库中的方法访问片段中的方法。我的代码如下所示:

Context c = getContext();

Class[] paramTypes = new Class[2];
paramTypes[0] = int.class;
paramTypes[1] = boolean.class;

java.lang.reflect.Method method = c.getClass().getMethod("someMethod", paramTypes);

method.invoke(c, id, selected);
如果方法“someMethod”被放置在fragment类中,则永远不会调用它。但是如果我把它放在片段所属的活动中,它就会被调用。getContext()方法似乎引用的是活动,而不是片段类


如何让它引用fragment类?但是,请记住,我的库可以在任何地方使用,包括活动本身。因此,我不想引用片段的上下文,而是引用正在使用库的任何类的上下文。

显然,您不能在xml中创建调用片段中click方法的click事件

见: