Constructor not visible

人走茶凉 提交于 2019-12-25 02:01:35

问题


I am developing an Android application which makes use of the ScanResult object. This object is in the form of:

[SSID: __mynetwork__, BSSID: 00:0e:2e:ae:4e:85, capabilities: [WPA-PSK-TKIP][ESS], level: -69, frequency: 2457, timestamp: 117455824743]

I am trying to override the equals() method of this class by creating my own class which extends ScanResult:

public class MyScanResult extends ScanResult {

    public MyScanResult() {
        super();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (!(o instanceof ScanResult))
            return false;

        ScanResult obj = (ScanResult) obj;
        if (!BSSID.equals(obj.BSSID))
            return false;
        if (!SSID.equals(obj.SSID))
            return false;
        if (!capabilities.equals(obj.capabilities))
            return false;
        if (frequency != obj.frequency)
            return false;
        if (level != obj.level)
            return false;

        return true;
    }

}

However when I try this, I get the following error: The constructor ScanResult() is not visible. How can I solve this please?


回答1:


The public constructor signature for ScanResult is:

public ScanResult(String SSID, String BSSID, String caps, int level, int frequency) 

You need to invoke the super class with matching parameters




回答2:


The apparently good way:

ScanResult expects parameters:

public ScanResult(  String SSID, 
                    String BSSID,
                    String caps,
                    int level,
                    int frequency)

You can check the class definition here

The ugly way:

As you say, apparently ScanResult is private. This answer tells you to use reflection to get to the constructor.

The possibly only way:

Nobody knows how this happens to you (it's actually weird). But there's a change to solve it. Hit the here link, copy the class into your project (change its package for yours, of course), and just make MyScanResult inherit from this one. Android is open source, and, despite this class may change in the future, you ensure this will work right now. Then, you can try casting your new ScanResult using (android.net.wifi.ScanResult)scanResult, if you need the primitive class.



来源:https://stackoverflow.com/questions/15886514/constructor-not-visible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!