1

I have written Inmobi Banner code but my app is crashing, please can anyone help what is wrong. MainActivity.java and Activity_main.xml codes are as below.

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private InMobiBanner mBanner;
    private static final String TAG = MainActivity.class.getName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        InMobiSdk.setLogLevel(InMobiSdk.LogLevel.DEBUG);
        mBanner = findViewById(R.id.banner);
        setupBannerAd();
        JSONObject consentObject = new JSONObject();
        try {
            // Provide correct consent value to sdk which is obtained by User
            consentObject.put(InMobiSdk.IM_GDPR_CONSENT_AVAILABLE, true);
            // Provide 0 if GDPR is not applicable and 1 if applicable
            consentObject.put("gdpr", "0");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        InMobiSdk.init(this, "a4d02aad2f14407782fdd6053fa5988f", consentObject, error -> {
            if (null != error) {
                Log.e(TAG, "InMobi Init failed -" + error.getMessage());
            } else {
                Log.d(TAG, "InMobi Init Successful");
            }
        });
    }

    private void setupBannerAd() {
        mBanner.setListener(new BannerAdEventListener() {
            @Override
            public void onAdLoadSucceeded(@NonNull InMobiBanner inMobiBanner,
                                          @NonNull AdMetaInfo adMetaInfo) {
                Log.d(TAG, "onAdLoadSucceeded with bid " + adMetaInfo.getBid());
            }

            @Override
            public void onAdLoadFailed(@NonNull InMobiBanner inMobiBanner, @NonNull InMobiAdRequestStatus inMobiAdRequestStatus) {
                Log.d(TAG, "Banner ad failed to load with error: " +
                        inMobiAdRequestStatus.getMessage());
            }

            @Override
            public void onAdClicked(@NonNull InMobiBanner inMobiBanner, @NonNull Map<Object, Object> map) {
                Log.d(TAG, "onAdClicked");
            }

            @Override
            public void onAdDisplayed(@NonNull InMobiBanner inMobiBanner) {
                Log.d(TAG, "onAdDisplayed");
            }

            @Override
            public void onAdDismissed(@NonNull InMobiBanner inMobiBanner) {
                Log.d(TAG, "onAdDismissed");
            }

            @Override
            public void onUserLeftApplication(@NonNull InMobiBanner inMobiBanner) {
                Log.d(TAG, "onUserLeftApplication");
            }

            @Override
            public void onRewardsUnlocked(@NonNull InMobiBanner inMobiBanner, @NonNull Map<Object, Object> map) {
                Log.d(TAG, "onRewardsUnlocked");
            }
        });
        mBanner.load();
        }
    }

ActivityMain.XML Code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:ads="http://schemas.android.com/apk/lib/com.inmobi.ads"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <com.inmobi.ads.InMobiBanner
        android:id="@+id/banner"
        android:layout_width="320dp"
        android:layout_height="50dp"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:placementId="plid-1638544811338"
        ads:refreshInterval="60"/>

</RelativeLayout>
James Z
  • 12,209
  • 10
  • 24
  • 44

1 Answers1

0

The problem is InMobi Banner expects InMobiSDK to be initialized, Now in the code if you see you have added InMobiBanner in xml and when you do setContentView the InMobiBanner will expect InMobiSDK to be initialized but the InMobiSDK code initialization is written after the setContentView. Hence it will throw an exception and your app will crash.

Solution would be to add init code before setContentView or to programmatically add Banner View to your UI

Raj Suvariya
  • 1,592
  • 3
  • 14
  • 36