【问题标题】:Constructor not visible构造函数不可见
【发布时间】:2013-04-08 18:41:28
【问题描述】:

我正在开发一个使用ScanResult 对象的Android 应用程序。该对象的形式为:

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

我正在尝试通过创建自己的扩展 ScanResult 的类来覆盖此类的 equals() 方法:

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;
    }

}

但是,当我尝试此操作时,我收到以下错误:The constructor ScanResult() is not visible。请问这个怎么解决?

【问题讨论】:

    标签: java android overriding


    【解决方案1】:

    ScanResult 的公共构造函数签名是:

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

    你需要用匹配的参数调用超类

    【讨论】:

    • The constructor ScanResult(String, String, String, int, int) is undefined
    • 这是source of ScanResult。如果您使用的是 Eclipse,请执行 CTRL+SPACE 并验证您是否匹配超类参数
    • 我做到了。我的 ScanResult 类具有以下构造函数:ScanResult();。问题是它不公开
    • android.net.wifi.ScanResult 没有任何默认构造函数,无论是私有的还是其他的。您使用的是当前版本吗?
    • 是的,我有最新的 SDK
    【解决方案2】:

    明显的好方法

    ScanResult 需要参数:

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

    可以查看类定义here

    丑陋的方式

    正如你所说,ScanResult 显然是私有的。 This answer 告诉你使用反射来获取构造函数。

    可能的唯一方法

    没有人知道这是怎么发生在你身上的(这实际上很奇怪)。但是有一个改变来解决它。点击here 链接,将该类复制到您的项目中(当然,为您的项目更改它的包),然后让MyScanResult 从这个继承。 Android 是开源的,尽管将来这个类可能会发生变化,但您要确保它现在可以工作。然后,如果您需要原始类,您可以尝试使用(android.net.wifi.ScanResult)scanResult 投射新的ScanResult

    【讨论】:

    • The constructor ScanResult(String, String, String, int, int) is undefined
    • 您使用的是哪个ScanResult?你在用com.amazonaws.services.dynamodb.model.ScanResult吗?
    • android.net.wifi.ScanResult
    • 好吧,最后一次机会,检查我的第二个(希望不是)或第三个解决方案。
    猜你喜欢
    • 1970-01-01
    • 2014-10-21
    • 1970-01-01
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    相关资源
    最近更新 更多