java.lang.NoSuchMethodError:没有虚方法getMccString()Ljava / lang / String;

问题描述 投票:1回答:1

我正在从CellInfo检索细胞信息数据(TelephonyManager

对于每个单元信息对象,我通过查询mcc(移动国家代码)和mnc(移动网络代码)

eachCellInfo.cellIdentity.mcc
eachCellInfo.cellIdentity.mnc

其中eachCellInfo是CellInfo的对象

根据文档不推荐使用该函数:

    /**
     * @return 2 or 3-digit Mobile Network Code, 0..999, Integer.MAX_VALUE if unknown
     * @deprecated Use {@link #getMncString} instead.
     */
    @Deprecated
    public int getMnc() {
        return (mMncStr != null) ? Integer.valueOf(mMncStr) : Integer.MAX_VALUE;
    }

但是当我使用建议的方法时,通过

    eachCellInfo.cellIdentity.mccString

方法描述:

        /**
         * @return Mobile Country Code in string format, null if unknown
         */
        public String getMccString() {
            return mMccStr;
        }

我正在关注崩溃日志:

java.lang.NoSuchMethodError: No virtual method getMccString()Ljava/lang/String; in class Landroid/telephony/CellIdentityLte; or its super classes (declaration of 'android.telephony.CellIdentityLte' appears in /system/framework/framework.jar!classes2.dex)
 )

如果我遗漏任何信息和可能的原因,请告诉我。

其他信息:

distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip 
kotlin_version = '1.3.21' 
classpath 'com.google.gms:google-services:4.2.0' 
classpath 'com.android.tools.build:gradle:3.3.1'
Debug Version
minifyEnabled false
shrinkResources false
android kotlin telephony telephonymanager
1个回答
3
投票

这个方法是在Android api 28中引入的 - 检查here - 这意味着它在以前的版本中不可用。

这将适用于运行api 28+的设备,并将在运行较低api级别的设备中抛出该异常。

通常,正确的方法是引入版本检查:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
   // Safe to use getMccString
} else {
   // Use something else that could work if there's something
}

请注意,仅仅因为您可以浏览计算机中的源代码并不意味着运行应用程序的设备将运行相同的Android代码 - 大多数情况下它不会运行。

© www.soinside.com 2019 - 2024. All rights reserved.