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

问题描述 投票:0回答:3

我正在努力在我的 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
    }
}
android flutter kotlin admob ads
3个回答
0
投票

改变

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

0
投票

我只是遇到了同样的错误,我将在这里发布我的发现。 您的错误与 LayoutInflater 有关;因此,您需要将 MainActivity 中的代码更改为如下所示:

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

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

0
投票

退出 Android 开发一段时间后也有点挣扎。

我出于其他原因使用

FlutterFragmentActivity
。对我来说关键是使用
layoutInflater
getter。

class MainActivity : FlutterFragmentActivity() {
    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        flutterEngine.plugins.add(GoogleMobileAdsPlugin())
        super.configureFlutterEngine(flutterEngine)

        GoogleMobileAdsPlugin.registerNativeAdFactory(
            flutterEngine,
            "adFactoryExample", NativeAdFactoryExample(layoutInflater)
        )
    }

    override fun cleanUpFlutterEngine(flutterEngine: FlutterEngine) {
        GoogleMobileAdsPlugin.unregisterNativeAdFactory(flutterEngine, "adFactoryExample")
        super.cleanUpFlutterEngine(flutterEngine)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.