【问题标题】:Add Native Ads to Flutter project using Kotlin使用 Kotlin 将原生广告添加到 Flutter 项目
【发布时间】:2021-09-15 22:03:16
【问题描述】:

我正在努力在我的颤振项目中添加原生广告。 我的项目使用 Kotlin for android,所以我遵循 google_mobile_ads 的文档 但在我使用 android studio 转换为 kotlin 的建议后,它一直向我显示这些错误。

e: reading_app\MainActivity.kt: (15, 113): No value passed for parameter 'layoutInflater'
e: reading_app\ReadingNativeAdFactory.kt: (15, 10): Class 'NativeAdFactoryExample' is not abstract and does not implement abstract member public abstract fun createNativeAd(p0: NativeAd!, p1: (Mutable)Map<String!, Any!>!): NativeAdView! defined in io.flutter.plugins.googlemobileads.GoogleMobileAdsPlugin.NativeAdFactory
e: reading_app\ReadingNativeAdFactory.kt: (20, 68): Unresolved reference: my_native_ad
e: reading_app\ReadingNativeAdFactory.kt: (21, 63): Unresolved reference: ad_headline
e: reading_app\ReadingNativeAdFactory.kt: (22, 59): Unresolved reference: ad_body

我的 MainActivity.kt 中的代码

package com.reading_app

import io.flutter.embedding.android.FlutterActivity


import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugins.googlemobileads.GoogleMobileAdsPlugin;


class MainActivity: FlutterActivity() {
    @Override
    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        flutterEngine.getPlugins().add(GoogleMobileAdsPlugin())
        super.configureFlutterEngine(flutterEngine)
        GoogleMobileAdsPlugin.registerNativeAdFactory(flutterEngine, "adFactoryExample", NativeAdFactoryExample())
    }

    @Override
    fun cleanUpFlutterEngine(flutterEngine: FlutterEngine?) {
        GoogleMobileAdsPlugin.unregisterNativeAdFactory(flutterEngine, "adFactoryExample")
    }
}

我的 ReadingNativeAdFactory.kt 中的代码

package com.reading_app

import android.graphics.Color
import android.view.LayoutInflater
import android.widget.TextView
import com.google.android.gms.ads.nativead.NativeAd
import com.google.android.gms.ads.nativead.NativeAdView
import io.flutter.plugins.googlemobileads.GoogleMobileAdsPlugin.NativeAdFactory
import java.util.Map


// my_native_ad.xml can be found at
/* https://github.com/googleads/googleads-mobile-flutter/tree/master/packages/google_mobile_ads/example/android/app/src/main/res/layout
*/
internal class NativeAdFactoryExample(layoutInflater: LayoutInflater) : NativeAdFactory {
    private val layoutInflater: LayoutInflater
    @Override
    fun createNativeAd(
            nativeAd: NativeAd, customOptions: Map<String?, Object?>?): NativeAdView {
        val adView: NativeAdView = layoutInflater.inflate(R.layout.my_native_ad, null) as NativeAdView
        val headlineView: TextView = adView.findViewById(R.id.ad_headline)
        val bodyView: TextView = adView.findViewById(R.id.ad_body)
        headlineView.setText(nativeAd.getHeadline())
        bodyView.setText(nativeAd.getBody())
        adView.setBackgroundColor(Color.YELLOW)
        adView.setNativeAd(nativeAd)
        adView.setBodyView(bodyView)
        adView.setHeadlineView(headlineView)
        return adView
    }

    init {
        this.layoutInflater = layoutInflater
    }
}

【问题讨论】:

    标签: android flutter kotlin admob ads


    【解决方案1】:

    改变

    code in my ReadingNativeAdFactory.kt
    

    package com.reading_app
    
    import android.graphics.Color
    import android.view.LayoutInflater
    import android.widget.TextView
    import com.google.android.gms.ads.nativead.NativeAd
    import com.google.android.gms.ads.nativead.NativeAdView
    import io.flutter.plugins.googlemobileads.GoogleMobileAdsPlugin.NativeAdFactory
    import kotlin.collections.Map
    
    internal class NativeAdFactoryLemoon(val layoutInflater: LayoutInflater) : NativeAdFactory {
        override fun createNativeAd(
            nativeAd: NativeAd, customOptions: Map<String, Any>?): NativeAdView  {
            val adView = layoutInflater.inflate(R.layout.my_native_ad, null) as NativeAdView 
            val headlineView = adView.findViewById<TextView>(R.id.ad_headline)
            val bodyView = adView.findViewById<TextView>(R.id.ad_body)
            headlineView.text = nativeAd.headline
            bodyView.text = nativeAd.body
            adView.setBackgroundColor(Color.BLUE)
            adView.setNativeAd(nativeAd)
            adView.bodyView = bodyView
            adView.headlineView = headlineView
            return adView
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-09-30
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-07
      • 2021-07-27
      • 2021-05-05
      • 1970-01-01
      相关资源
      最近更新 更多