Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在继承方法上使用反射的正确方法_Android_Reflection - Fatal编程技术网

Android 在继承方法上使用反射的正确方法

Android 在继承方法上使用反射的正确方法,android,reflection,Android,Reflection,我在我的应用程序中使用了谷歌音乐应用程序中的TouchInterceptor类。此类允许您将列表项拖放到列表中的不同位置 TouchInterceptor类调用名为smoothScrollBy的方法。此方法仅在API 8+中可用 我希望我的应用程序以API 7+为目标,所以我需要使用反射来执行smoothScrollBy,前提是它存在 在TouchInterceptor的构造函数中,我添加了以下内容: Method[] ms = this.getClass().getDeclaredMe

我在我的应用程序中使用了谷歌音乐应用程序中的TouchInterceptor类。此类允许您将列表项拖放到列表中的不同位置

TouchInterceptor类调用名为smoothScrollBy的方法。此方法仅在API 8+中可用

我希望我的应用程序以API 7+为目标,所以我需要使用反射来执行smoothScrollBy,前提是它存在

在TouchInterceptor的构造函数中,我添加了以下内容:

    Method[] ms = this.getClass().getDeclaredMethods();
    if (ms != null) {
        for (Method m : ms) {
            if (m != null && m.toGenericString().equals("smoothScrollBy")) {
                Class[] parameters = m.getParameterTypes();
                if (parameters != null && parameters.length == 1 && parameters[0].getName().equals("int")) {
                    mSmoothScrollBy = m;
                }
            }
        }
    }
    try {
        mSmoothScrollBy = this.getClass().getMethod("smoothScrollBy", new Class[] { int.class, int.class });
    }
    catch (NoSuchMethodException e) {

    }
这应该找到smoothScrollBy方法,并将其分配给名为mSmoothScrollBy(method)的TouchInterceptor的新成员变量

我正在安卓2.2(API 8)模拟器上调试,不幸的是,没有找到该方法。我猜getDeclaredMethods()不会在数组中返回它,因为smoothScrollBy是AbsListView的一个方法,它由ListView继承,最终是TouchInterceptor

在调用getClass().getDeclaredMethods()之前,我已尝试将其强制转换为AbsListView,但没有成功

我如何才能正确地获得smoothScrollBy,以便在可用时调用它

更新:

我还尝试了以下方法,但没有成功:

        Method test = null;
        try {
            test = this.getClass().getMethod("smoothScrollBy", new Class[] { Integer.class });
        }
        catch (NoSuchMethodException e) {

        }

我不确定,但我认为如果您将应用程序定位到API 7,那么将找不到该方法,因为它不存在。您可以将API 8作为目标,并在清单中列出您只需要API级别7的列表。

创建一个名为
hasMethod(Class cls,String method)
或类似的方法,该方法递归调用继承层次结构:

public boolean hasMethod(Class cls, String method) {
    // check if cls has the method, if it does return true
    // if cls == Object.class, return false
    // else, make recursive call
    return hasMethod(cls.getSuperclass(), method);
}

这是因为它是一个继承的方法
getDeclaredMethods()
仅检索在类中声明的方法,而不是其超类的方法。虽然我从未真正这样做过,但您应该能够调用
getSuperclass()
,直到找到声明方法的类(
AbsListView
)并从中获取方法


一个更简单的答案可能只是检查API的版本:

谢谢您的回复。我已通过执行以下操作解决了此问题:

    Method[] ms = this.getClass().getDeclaredMethods();
    if (ms != null) {
        for (Method m : ms) {
            if (m != null && m.toGenericString().equals("smoothScrollBy")) {
                Class[] parameters = m.getParameterTypes();
                if (parameters != null && parameters.length == 1 && parameters[0].getName().equals("int")) {
                    mSmoothScrollBy = m;
                }
            }
        }
    }
    try {
        mSmoothScrollBy = this.getClass().getMethod("smoothScrollBy", new Class[] { int.class, int.class });
    }
    catch (NoSuchMethodException e) {

    }
我寻找的方法的参数列表不正确