0

ボタンがあり、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;
    }

関数が実際には見つかりません。ここで誤解があることは明らかです。この方法に到達することは不可能でしょうか?? 前もって感謝します。

4

2 に答える 2

0

何がわからない

if(arrMethods[i].getName() == MethodName)

本当にあなたが欲しいものです。この条件は、arrMethods [i] .getName()がMethodNameによって参照される同じオブジェクトへの参照を返す場合に当てはまります。

文字列が等しいかどうかを確認すると思います。

if(arrMethods[i].getName().equals(MethodName)
于 2012-03-21T19:08:30.023 に答える