简体   繁体   中英

Error inflating class com.inmobi.ads.InMobiBanner

I followed the InMobi guidelines for Android apps step by step but got the following exception from
DataBindingUtil.setContentView(this, R.layout.mainActivity):

Binary XML file line #670: Binary XML file line #670: Error inflating class com.inmobi.ads.InMobiBanner

<LinearLayout
    xmlns:inmobi="http://schemas.android.com/apk/lib/com.inmobi.ads"
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical">
    <TextView android:text="Ad"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <com.inmobi.ads.InMobiBanner
        android:id="@+id/bannerAdInMobi"
        android:layout_width="320dp"
        android:layout_height="50dp"
        inmobi:placementId="plid-1526902340491"
        inmobi:refreshInterval="60" />
</LinearLayout>

Could anyone offer a tip on how to diagnose it?

Since no one answered after a month, let me post mine. InMobil's support has kindly helped me find the culprit - layout inflation before InMobiSdk.init() . An app needs to run InMobiSdk.init() before inflating the layout.

My app cannot do this because it uses InMobil ads only under certain conditions (eg only the FREE version has ads), so I have removed com.inmobi.ads.InMobiBanner from the layout, and add it in Java code when it is needed.

Ok I Solved this problem. just follow these steps ->

Step 1: Declare the variable in your Activity/fragment

private lateinit var bannerAd: InMobiBanner

Step 2: Initialite InMobiSDK in onCreate()

override fun onCreate(savedInstanceState: Bundle?) {
    bannerAd= Helper.InitializInMobiAds(requireContext())
    super.onCreate(savedInstanceState)
}

Step 3: here is InitializInMobiAds() method in Helper Class

 fun InitializInMobiAds(context: Context): InMobiBanner{
        val consentObject = JSONObject()
        try {
            // Provide correct consent value to sdk which is obtained by User
            consentObject.put(InMobiSdk.IM_GDPR_CONSENT_AVAILABLE, false)
            // Provide 0 if GDPR is not applicable and 1 if applicable
            consentObject.put("gdpr", "0")
            // Provide user consent in IAB format
            //  consentObject.put(InMobiSdk.IM_GDPR_CONSENT_IAB, “ << consent in IAB format >> ”)
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        InMobiSdk.init(context, "AcountIdHere" , consentObject,  SdkInitializationListener() {
            @Override
            fun onInitializationComplete(error : Error?) {
                if (null != error) {
                    Log.e("", "InMobi Init failed -" + error.message.toString())
                } else {
                    Log.d("", "InMobi Init Successful")
                }
            }
        })

       return  InMobiBanner(context, PLID_here)
    }

Step 4: now use.load() method in onCreateView() or in whichever block which runs after onCreate()

 binding.adView.load()
 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM