javaandroidmapkit

java.lang.AssertionError: setApiKey() should be called before initialize()! | Switching from SD 33 to SDK 34 Yandex MapKit


When changing the target SDK from 33 to 34, the activity with the map began to crash the application, during the checks it turned out that the version of the MapKit library requires an update, it was updated to the latest 3.5, and yes, the application began to open the activity, but only once, reopening leads to the error "java.lang.AssertionError: setApiKey() should be called before initialize()!"

An excerpt of the code in which the crash occurs:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MapKitFactory.setApiKey(getString(R.string.yandex_key));
        MapKitFactory.initialize(this);
        setContentView(R.layout.activity_yandex_map);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mapview.onStart();
        MapKitFactory.getInstance().onStart();
        updateViews();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mapview.onStop();
        MapKitFactory.getInstance().onStop();
        locationManager.removeUpdates(locationListener);
        locationListener = null;
    }

Stacktrace:

> FATAL EXCEPTION: main
> java.lang.AssertionError: setApiKey() should be called before initialize()!
> at com.yandex.mapkit.MapKitFactory.setApiKey(MapKitFactory.java:35)
> at activities.AddressesSearchBoth.onCreate(AddressesSearchBoth.java:101)
> at android.app.Activity.performCreate(Activity.java:8975)
> at android.app.Activity.performCreate(Activity.java:8944)
> at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1456)
> at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4146)
> at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4322)
> at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
> at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:139)
> at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96)
> at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2685)
> at android.os.Handler.dispatchMessage(Handler.java:106)
> at android.os.Looper.loopOnce(Looper.java:230)
> at android.os.Looper.loop(Looper.java:319)
> at android.app.ActivityThread.main(ActivityThread.java:8919)
> at java.lang.reflect.Method.invoke(Native Method)
> at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:578)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)

I studied the documentation, but did not find any solutions to this problem (most likely, due to my inexperience, I do not see a trivial problem)


Solution

  • What you have there does not initialize - and the current version would be 4.7.0.
    While all the tutorials set the key once in Application and not in the Activity ...