繁体   English   中英

使用 Kotlin 将原生广告添加到 Flutter 项目

[英]Add Native Ads to Flutter project using Kotlin

我正在努力在我的 flutter 项目中添加原生广告。 我的项目使用 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
    }
}

改变

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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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