Home » Android » java – Why .baseUrl(URL) crash my App while I build a Retrofit object?-Exceptionshub

java – Why .baseUrl(URL) crash my App while I build a Retrofit object?-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I have such code. I try to create Retrofit Builder, but the app crashes at the string .baseUrl(BASE_URL) and I don’t know why.

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import ru.okcode.news.model.endpoints.TopNewsEndpoints;

public class RestApi {

private static final String BASE_URL = "https://api.nytimes.com/svc/topstories/v2/";
private static final String API_KEY = "lvoWYBxHw7wX3hLFntwUA1XhkuCZoOqE";

private static RestApi instance;

private TopNewsEndpoints topNewsEndpoints;

private RestApi() {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    topNewsEndpoints = retrofit.create(TopNewsEndpoints.class);
}

public static synchronized RestApi getInstance() {
    if (instance == null) {
        instance = new RestApi();
    }
    return instance;
}

public TopNewsEndpoints news() {
    return topNewsEndpoints;
}
}

Error log is such:

2020-02-18 11:05:06.621 6896-6896/ru.okcode.news E/AndroidRuntime: FATAL EXCEPTION: main
Process: ru.okcode.news, PID: 6896
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{ru.okcode.news/ru.okcode.news.NewsListActivity}: java.lang.ClassNotFoundException: Didn't find class "ru.okcode.news.NewsListActivity" on path: DexPathList[[zip file "/data/app/ru.okcode.news-THvZ3i1rlZ8ujPZ4Sf_BaA==/base.apk"],nativeLibraryDirectories=[/data/app/ru.okcode.news-THvZ3i1rlZ8ujPZ4Sf_BaA==/lib/x86, /system/lib, /vendor/lib]]
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2718)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "ru.okcode.news.NewsListActivity" on path: DexPathList[[zip file "/data/app/ru.okcode.news-THvZ3i1rlZ8ujPZ4Sf_BaA==/base.apk"],nativeLibraryDirectories=[/data/app/ru.okcode.news-THvZ3i1rlZ8ujPZ4Sf_BaA==/lib/x86, /system/lib, /vendor/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1173)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2708)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
    at android.os.Handler.dispatchMessage(Handler.java:105) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6541) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
    Suppressed: java.io.IOException: Failed to open dex files from /data/app/ru.okcode.news-THvZ3i1rlZ8ujPZ4Sf_BaA==/base.apk because: Failure to verify dex file '/data/app/ru.okcode.news-THvZ3i1rlZ8ujPZ4Sf_BaA==/base.apk': Bad method handle type 7
    at dalvik.system.DexFile.openDexFileNative(Native Method)
    at dalvik.system.DexFile.openDexFile(DexFile.java:353)
    at dalvik.system.DexFile.<init>(DexFile.java:100)
    at dalvik.system.DexFile.<init>(DexFile.java:74)
    at dalvik.system.DexPathList.loadDexFile(DexPathList.java:374)
    at dalvik.system.DexPathList.makeDexElements(DexPathList.java:337)
    at dalvik.system.DexPathList.<init>(DexPathList.java:157)
    at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:65)
    at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:64)
    at com.android.internal.os.PathClassLoaderFactory.createClassLoader(PathClassLoaderFactory.java:43)
    at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:69)
    at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:36)
    at android.app.LoadedApk.createOrUpdateClassLoaderLocked(LoadedApk.java:676)
    at android.app.LoadedApk.getClassLoader(LoadedApk.java:709)
    at android.app.LoadedApk.getResources(LoadedApk.java:936)
    at android.app.ContextImpl.createAppContext(ContextImpl.java:2242)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5672)
    at android.app.ActivityThread.-wrap1(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1661)

Could you help me with this issue?

How to&Answers: