简体   繁体   English

Java反射 - 参数数量错误; 预期0,得1

[英]Java Reflection - Wrong number of arguments; expected 0, got 1

I am running this java code 我正在运行这个java代码

ConnectivityManager connectivityManager = ((ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE));
    try {
        Method method = connectivityManager.getClass().getDeclaredMethod("getTetherableIfaces");
        String[] strings = ((String[]) method.invoke(connectivityManager));
        Log.i("hotspot", "getIface: "+strings.toString());
        Method methodTether = connectivityManager.getClass().getDeclaredMethod("tether",String.class);
        methodTether.setAccessible(true);
        String[] param =new String[]{"wlan0"};

        int i = (int) method.invoke(connectivityManager,"wlan0");
        Log.i(TAG, "getIface: "+ "errorcode"+ i);

    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }

but i get this error 但我得到这个错误

 java.lang.IllegalArgumentException: Wrong number of arguments; expected 0, got 1
    at java.lang.reflect.Method.invoke(Native Method)

And this is the tether function which i am trying to invoke. 这是我试图调用的系绳功能。

 public int tether(String iface) {
    try {
        return mService.tether(iface);
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
}

I tried to invoke the method with object[]{"wlan0"}, String[]{"wlan0"}, (object){"wlan0"}, {"wlan0"} and (Object[])String[]{"wlan0"} but I get the same error. 我试图用object[]{"wlan0"}, String[]{"wlan0"}, (object){"wlan0"}, {"wlan0"}(Object[])String[]{"wlan0"}调用该方法。 (Object[])String[]{"wlan0"}但是我得到了同样的错误。 I am not able to understand what am I doing wrong. 我无法理解我做错了什么。 For Any help I would be thankful. 对于任何帮助,我将感激不尽。

The error says "Wrong number of arguments; expected 0, got 1". 错误说“参数数量错误;预期为0,得1”。 That means the method you are calling is not the one you think it is. 这意味着您调用的方法不是您认为的方法。 The method being called does not have any arguments and you are passing an argument to it. 被调用的方法没有任何参数,你正在向它传递一个参数。

You are invoking method instead of methodTether . 您正在调用method而不是methodTether

In the line 在线

Method method = connectivityManager.getClass().getDeclaredMethod("getTetherableIfaces");

method.invoke() will now call getTetherableIfaces() as invoke() : method.invoke()现在会调用getTetherableIfaces()invoke()

Invokes the underlying method represented by this Method object, on the specified object with the specified parameters 在具有指定参数的指定对象上调用此Method对象表示的基础方法

which looks like a getter method and would thus accept no parameters. 它看起来像一个getter方法,因此不接受任何参数。 Then you try to pass an argument which will cause this error 然后你尝试传递一个会导致这个错误的参数

String[] strings = ((String[]) method.invoke(connectivityManager));

It looks like you meant to call methodTether.invoke() 看起来你打算调用methodTether.invoke()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM