Reputation: 5
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?
Upvotes: 0
Views: 3078
Reputation: 1720
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
Upvotes: 2