InMobi / sdk-sample-code-android

This repository contains InMobi Android SDK sample code for different ad-formats, which publishers can use to incorporate in their apps — Edit
Apache License 2.0
38 stars 43 forks source link

SdkNotInitializedException #62

Closed aniketshewale85 closed 2 years ago

aniketshewale85 commented 4 years ago

I have created banner Ad programmatically. But it is crashing on some of the devices with error SdkNotInitializedException. I have initialized SDK on Main Activity and creating a banner on the second activity.

im-lakshya commented 4 years ago

Hey @aniketshewale85 ,

Can you share the logs, I think there is some dependency which you missed adding into build.gradle due to which InMobi SDK is not initialized.

aniketshewale85 commented 4 years ago

This is my Logs I am using admob consent SDK to determine whether user is located in EU or not. And using Shared Preferences to save Inmobi SDK initialized successfully or not. If successful, requesting banner and interstitial Ad in another activity

Caused by com.inmobi.ads.exceptions.SdkNotInitializedException Please initialize the SDK before creating InMobiBanner ad com.inmobi.ads.InMobiBanner. (InMobiBanner.java:201) com.anitech.puzzlegame.brainmaster.smartcalculation.Addition.onCreate (Addition.java:90) android.app.Activity.performCreate (Activity.java:7166) android.app.Activity.performCreate (Activity.java:7157) android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1220) android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2854) android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2979) android.app.ActivityThread.-wrap11 android.app.ActivityThread$H.handleMessage (ActivityThread.java:1683) android.os.Handler.dispatchMessage (Handler.java:106) android.os.Looper.loop (Looper.java:192) android.app.ActivityThread.main (ActivityThread.java:6754) java.lang.reflect.Method.invoke (Method.java) com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:549) com.android.internal.os.ZygoteInit.main (ZygoteInit.java:828)

im-lakshya commented 4 years ago

Can you share the full logs by enabling InMobi DEBUG logs InMobiSdk.setLogLevel(LogLevel.DEBUG); and after then you callInMobiSdk.init(..) you should get something like InMobi SDK initialized with account id: in your logs. If you're not getting this then there is something wrong with the integration, and you should get the error log which should explain why sdk initialization failed. Which InMobi SDK version you're using?

aniketshewale85 commented 4 years ago

In My device it is working fine I received these crash events Firebase Crashilytics

im-lakshya commented 4 years ago

@aniketshewale85 Are you using the same shared preferences across the sessions to determine whether InMobi SDK is initialized or not?

aniketshewale85 commented 4 years ago

No, I reset shared pref at app launch and then initialize Inmobi SDK

im-lakshya commented 4 years ago

Can you share some code snippets on how you're initializing the InMobi SDK and saving/clear part of preferences. Or some sample app would be great which does the same thing as your app is doing.

aniketshewale85 commented 4 years ago

This is on App Launcher Activity SharedPreferences preferences1 = getSharedPreferences("INMOBI_INIT_PREF", MODE_PRIVATE); SharedPreferences.Editor editor = preferences1.edit(); editor.putBoolean("SUCCESS", false); editor.apply();

Following is in Main Activity

private void getConsentStatus() {

    ConsentInformation consentInformation = ConsentInformation.getInstance(this);
    String[] publisherIds = {"Admob Publisher ID"};
    consentInformation.requestConsentInfoUpdate(publisherIds, new ConsentInfoUpdateListener() {
        @Override
        public void onConsentInfoUpdated(ConsentStatus consentStatus) {

            // User's consent status successfully updated.

            if (ConsentInformation.getInstance(getBaseContext()).isRequestLocationInEeaOrUnknown()) {

                switch (consentStatus) {
                    case UNKNOWN:

                        List<AdProvider> adProviders =
                                ConsentInformation.getInstance(getApplicationContext()).getAdProviders();

                        displayConsentForm();

                        break;
                    case PERSONALIZED:

                        initializeAds();

                        inMobiConsent(true);
                        break;

                    case NON_PERSONALIZED:
                        initializeAds();

                        inMobiConsent(false);
                        break;
                }

            } else {
                // Log.d(TAG, "Not in EU, displaying normal ads");

                initializeAds();  //Admob   
                inMobiConsent(true);              

            }

        }

        @Override
        public void onFailedToUpdateConsentInfo(String errorDescription) {
            // User's consent status failed to update.
            initializeAds(); // Admob

        }
    });
}

private void inMobiConsent(boolean consentAvailable) {

    consentObject = new JSONObject();
    try {
        // Provide correct consent value to sdk which is obtained by User
        consentObject.put(InMobiSdk.IM_GDPR_CONSENT_AVAILABLE, consentAvailable);

    } catch (JSONException e) {
        e.printStackTrace();
    }

    InMobiSdk.init(this, "3ae07cff36a548b89f351275b13f8515", consentObject, new SdkInitializationListener() {
        @Override
        public void onInitializationComplete(@Nullable Error error) {
            if (null != error) {
                Log.e("Inmobi", "InMobi Init failed -" + error.getMessage());

            } else {
                Log.d("Inmobi", "InMobi Init Successful");

                SharedPreferences preferences = getSharedPreferences("INMOBI_INIT_PREF", MODE_PRIVATE);
                SharedPreferences.Editor editor = preferences.edit();
                editor.putBoolean("SUCCESS", true);
                editor.apply();
            }
        }
    });

}
RajSuvariya-InMobi commented 4 years ago

@aniketshewale85

Looks like it is happening due to onFailedToUpdateConsentInfo. You are not initializing InMobi if the consent update fails.

        @Override
        public void onFailedToUpdateConsentInfo(String errorDescription) {
            // User's consent status failed to update.
            initializeAds(); // Admob

        }
aniketshewale85 commented 4 years ago

I have added in App. I forgot to copy here

@Override public void onFailedToUpdateConsentInfo(String errorDescription) { // User's consent status failed to update. initializeAds(); // Admob inMobiConsent(true); }

sunilkuna5-inmobi commented 2 years ago

@aniketshewale85 Closing the issue as the issue was raised 2 years ago. Please re-open the issue if the issue persists.