英文:
Fatal Exception: java.lang.NoSuchMethodError
问题
我正在处理这个问题:
致命异常:java.lang.NoSuchMethodError
类别android/net/ConnectivityManager中没有名为getActiveNetwork()的虚拟方法Landroid/net/Network; 或者在它的超类中('android.net.ConnectivityManager'的声明出现在/system/framework/framework.jar:classes2.dex中)
这是我的代码:
public boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
if (capabilities != null) {
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
return true;
} else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
return true;
} else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
return true;
}
}
}
}
return false;}
这是堆栈跟踪:
致命异常:java.lang.NoSuchMethodError:类别android/net/ConnectivityManager中没有名为getActiveNetwork()的虚拟方法Landroid/net/Network; 或者在它的超类中('android.net.ConnectivityManager'的声明出现在/system/framework/framework.jar:classes2.dex中)
at com.olgazelenko.esofer.MainMenu.isNetworkAvailable(MainMenu.java:307)
at com.olgazelenko.esofer.MainMenu.onCreate(MainMenu.java:75)
at android.app.Activity.performCreate(Activity.java:6178)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2648)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
有人知道如何修复这个问题吗?
英文:
I am dealing with that problem:
Fatal Exception: java.lang.NoSuchMethodError
No virtual method getActiveNetwork()Landroid/net/Network; in class Landroid/net/ConnectivityManager; or its super classes (declaration of 'android.net.ConnectivityManager' appears in /system/framework/framework.jar:classes2.dex)
There is my code:
public boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
if (capabilities != null) {
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
return true;
} else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
return true;
} else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
return true;
}
}
}
}
return false;}
there is the stacktrace:
Fatal Exception: java.lang.NoSuchMethodError: No virtual method getActiveNetwork()Landroid/net/Network; in class Landroid/net/ConnectivityManager; or its super classes (declaration of 'android.net.ConnectivityManager' appears in /system/framework/framework.jar:classes2.dex)
at com.olgazelenko.esofer.MainMenu.isNetworkAvailable(MainMenu.java:307)
at com.olgazelenko.esofer.MainMenu.onCreate(MainMenu.java:75)
at android.app.Activity.performCreate(Activity.java:6178)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2648)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
does someone know how that can be fixed?
答案1
得分: 1
这个错误经常在你使用某个版本的依赖进行编译,但在运行代码时使用了不同版本的情况下出现。
在这里,看起来 com.olgazelenko.esofer.MainMenu.isNetworkAvailable(MainMenu.java:307)
调用了一个来自Android的方法,在你的执行过程中未找到。
这个方法,getActiveNetwork
,似乎是在Android SDK API Level 23中添加的。你可能尝试在一个较旧的版本中使用它。请查看 https://developer.android.com/reference/android/net/ConnectivityManager#getActiveNetwork()。尝试升级你的 minSdkVersion
:https://developer.android.com/guide/topics/manifest/uses-sdk-element。
英文:
This Error occurs often when you have compiled with one version of your dependency but are running the code with a different version.
Here, it looks that com.olgazelenko.esofer.MainMenu.isNetworkAvailable(MainMenu.java:307)
calls a method from android which is not found during your execution.
This method, getActiveNetwork, seemed to be added in Android SDK API Level 23. You maybe try to use it in a older version. see https://developer.android.com/reference/android/net/ConnectivityManager#getActiveNetwork(). Try to upgrade your minSdkVersion : https://developer.android.com/guide/topics/manifest/uses-sdk-element
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论