Meh*_*may 6 android flutter google-ads-api
我在 flutter 原生广告 (android) 上遇到此错误:
\nPlatformException(NativeAdError, Can't find NativeAdFactory with id: homeScreen, null, null)
google_mobile_ads: ^0.13.0\n
Run Code Online (Sandbox Code Playgroud)\n这是我的 HomeScreenNativeAd.kt 文件
\nclass HomeScreenNativeAd(val context: Context) : GoogleMobileAdsPlugin.NativeAdFactory {\n\n override fun createNativeAd(\n nativeAd: NativeAd,\n customOptions: MutableMap<String, Any>?\n ): NativeAdView {\n val homeScreenNativeView = LayoutInflater.from(context)\n .inflate(R.layout.home_screen_native_ad, null) as NativeAdView\n\n with(homeScreenNativeView) {\n val attributionViewSmall =\n findViewById<TextView>(R.id.tv_home_item_native_ad_attribution_small)\n\n\n val icon = findViewById<ImageView>(R.id.home_item_bg_image)\n\n\n val image = nativeAd.icon\n if (image != null) {\n attributionViewSmall.visibility = View.VISIBLE\n icon.setImageDrawable(image.drawable)\n } else {\n attributionViewSmall.visibility = View.INVISIBLE\n }\n this.iconView = iconView\n\n val adV = findViewById<TextView>(R.id.adviser)\n adV.text = nativeAd.advertiser\n this.advertiserView =adV\n\n val ctaButton = findViewById<TextView>(R.id.home_screen_cta_button)\n ctaButton.text = nativeAd.callToAction\n this.callToActionView = ctaButton\n\n val adDesc = findViewById<TextView>(R.id.home_item_ad_desc)\n adDesc.text = nativeAd.body\n this.bodyView = ctaButton\n\n\n\n val headlineView = findViewById<TextView>(R.id.home_item_ad_title)\n headlineView.text = nativeAd.headline\n this.headlineView = headlineView\n\n\n\n\n setNativeAd(nativeAd)\n }\n\n return homeScreenNativeView\n }\n}\n
Run Code Online (Sandbox Code Playgroud)\nhome_screen_native_ad.xml 文件
\n`
\n<com.google.android.gms.ads.nativead.NativeAdView\n xmlns:android="http://schemas.android.com/apk/res/android"\n xmlns:app="http://schemas.android.com/apk/res-auto"\n xmlns:tools="http://schemas.android.com/tools"\n android:layout_width="match_parent"\n android:layout_height="match_parent">\n <FrameLayout\n android:layout_width="match_parent"\n android:layout_height="match_parent">\n <RelativeLayout\n android:orientation="vertical"\n android:layout_width="wrap_content"\n\n android:layout_height="160dp">\n\n\n\n <FrameLayout\n android:layout_width="match_parent"\n android:background="#00ffffff"\n android:layout_height="140dp"/>\n\n <RelativeLayout\n\n android:layout_width="match_parent"\n android:layout_height="190dp"\n android:layout_marginTop="16dp"\n android:layout_marginEnd="16dp"\n android:layout_marginBottom="16dp"\n android:gravity="start"\n android:orientation="horizontal">\n\n <ImageView\n android:id="@+id/home_item_bg_image"\n android:layout_width="60dp"\n android:layout_height="60dp"\n android:scaleType="centerCrop"\n android:layout_marginLeft="16dp"\n\n\n />\n\n\n <TextView\n android:id="@+id/home_item_ad_title"\n android:layout_width="220dp"\n\n android:layout_height="wrap_content"\n android:layout_marginLeft="10dp"\n android:layout_toRightOf="@+id/home_item_bg_image"\n android:ellipsize="end"\n android:lines="1"\n android:maxLines="1"\n android:textColor="#FFFFFFFF"\n android:textSize="20sp"\n tools:text="Headline" />\n\n <TextView\n android:id="@+id/adviser"\n android:layout_width="229dp"\n android:layout_height="wrap_content"\n android:layout_marginLeft="10dp"\n android:layout_marginTop="32dp"\n android:layout_toRightOf="@+id/home_item_bg_image"\n android:ellipsize="end"\n android:lines="1"\n android:maxLines="1"\n android:textColor="#FFFFFFFF"\n android:textSize="14sp"\n tools:text="Advertiser" />\n\n\n <TextView\n android:id="@+id/tv_home_item_native_ad_attribution_small"\n android:layout_width="35dp"\n android:layout_height="20dp"\n android:layout_marginLeft="10dp"\n android:layout_toRightOf="@+id/home_item_ad_title"\n android:background="@color/softOrange"\n android:gravity="center"\n android:lines="1"\n android:maxLines="1"\n android:text="Ad"\n android:textColor="#FFFF"\n android:textSize="12sp" />\n\n\n\n </RelativeLayout>\n\n <TextView\n android:id="@+id/home_item_ad_desc"\n android:layout_width="270dp"\n android:layout_height="40dp"\n android:layout_marginTop="85dp"\n android:ellipsize="end"\n android:lines="2"\n android:gravity="center_vertical"\n android:maxLines="2"\n android:textColor="#FFFFFFFF"\n android:textSize="14sp"\n android:layout_marginLeft="16dp"\n tools:text="Headline" />\n\n <TextView\n android:id="@+id/home_screen_cta_button"\n android:layout_width="wrap_content"\n android:layout_marginLeft="10dp"\n android:layout_height="30dp"\n android:layout_toRightOf="@+id/home_item_ad_desc"\n android:layout_marginTop="100dp"\n android:text="INSTALL"\n android:gravity="center"\n android:ellipsize="end"\n android:lines="1"\n android:textColor="#FFF"\n android:maxLines="1"\n android:textSize="18sp"\n />\n\n\n </RelativeLayout>\n </FrameLayout>\n</com.google.android.gms.ads.nativead.NativeAdView>`\n
Run Code Online (Sandbox Code Playgroud)\nMainActivity.kt
\n`class MainActivity : FlutterActivity() {\n private val CHANNEL = "verified.cv/channel" //The channel name you set in your main.dart file\n\n override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {\n super.configureFlutterEngine(flutterEngine)\n MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {\n // Note: this method is invoked on the main thread.\n call, result ->\n\n if (call.method == "createNotificationChannel") {\n val argData = call.arguments as java.util.HashMap<String, String>\n val completed = createNotificationChannel(argData)\n if (completed == true) {\n result.success(completed)\n } else {\n result.error("Error Code", "Error Message", null)\n }\n } else {\n result.notImplemented()\n }\n }\n GoogleMobileAdsPlugin.registerNativeAdFactory(\n flutterEngine, "listTile", ListTileNativeAdFactory(context))\n GoogleMobileAdsPlugin.registerNativeAdFactory(\n flutterEngine, "skillCard", SkillCardNativeFactory(context))\n GoogleMobileAdsPlugin.registerNativeAdFactory(\n flutterEngine, "homeScreen", HomeScreenNativeAd(context))\n GoogleMobileAdsPlugin.registerNativeAdFactory(\n flutterEngine, "feedItem", FeedItemNativeFactory(context))\n\n\n }\n\n override fun cleanUpFlutterEngine(flutterEngine: FlutterEngine) {\n super.cleanUpFlutterEngine(flutterEngine)\n\n // TODO: Unregister the ListTileNativeAdFactory\n GoogleMobileAdsPlugin.unregisterNativeAdFactory(flutterEngine, "listTile")\n GoogleMobileAdsPlugin.unregisterNativeAdFactory(flutterEngine, "skillCard")\n GoogleMobileAdsPlugin.unregisterNativeAdFactory(flutterEngine, "homeScreen")\n GoogleMobileAdsPlugin.unregisterNativeAdFactory(flutterEngine, "feedItem")\n\n\n\n }\n\n private fun createNotificationChannel(mapData: HashMap<String, String>): Boolean {\n val completed: Boolean\n if (VERSION.SDK_INT >= VERSION_CODES.O) {\n // Create the NotificationChannel\n val id = mapData["id"]\n val name = mapData["name"]\n val descriptionText = mapData["description"]\n // val sound = "your_sweet_sound"\n val importance = NotificationManager.IMPORTANCE_HIGH\n val mChannel = NotificationChannel(id, name, importance)\n mChannel.description = descriptionText\n\n // val soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getApplicationContext().getPackageName() + "/raw/your_sweet_sound");\n // val att = AudioAttributes.Builder()\n // .setUsage(AudioAttributes.USAGE_NOTIFICATION)\n // .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)\n // .build();\n\n //mChannel.setSound(soundUri, att)\n // Register the channel with the system; you can't change the importance\n // or other notification behaviors after this\n val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager\n notificationManager.createNotificationChannel(mChannel)\n completed = true\n } else {\n completed = false\n }\n return completed\n }\n}\n`\n
Run Code Online (Sandbox Code Playgroud)\n\nI/flutter ( 1409): ----------------FIREBASE CRASHLYTICS----------------\nI/flutter ( 1409): PlatformException(NativeAdError, Can't find NativeAdFactory with id: homeScreen, null, null)\nI/flutter ( 1409): #0 StandardMethodCodec.decodeEnvelope\npackage:flutter/\xe2\x80\xa6/services/message_codecs.dart:607\nI/flutter ( 1409): #1 MethodChannel._invokeMethod\npackage:flutter/\xe2\x80\xa6/services/platform_channel.dart:177\nI/flutter ( 1409): <asynchronous suspension>\nI/flutter ( 1409): #2 NativeAd.load\npackage:google_mobile_ads/src/ad_containers.dart:1033\nI/flutter ( 1409): <asynchronous suspension>\nI/flutter ( 1409): ----------------------------------------------------\nD/ACodec ( 1409): dataspace changed to 0x10c40000 (R:2(Limited), P:4(BT601_6_525), M:3(BT601_6), T:3(SMPTE170M)) (\n
Run Code Online (Sandbox Code Playgroud)\n
归档时间: |
|
查看次数: |
1280 次 |
最近记录: |