android - 即使使用测试广告单元,AdFailedToLoad 也会出现“无广告配置”错误
问题描述
安卓(Java)方式:
最小的可重现代码:
public class MainActivity extends Activity {
private static final String TAG = "MyTag";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this);
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(this, "ca-app-pub-3940256099942544/1033173712", adRequest,
new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
Log.i(TAG, "onAdLoaded");
interstitialAd.show(MainActivity.this);
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
Log.i(TAG, loadAdError.getMessage());
}
});
}
}
我的build.gradle(app)
implementation 'com.google.android.gms:play-services-ads:20.2.0'
我的AndroidManifest.xml
:
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
问题(在 Android 上):
即使使用测试广告 ID 和测试应用 ID,广告也不会加载。
Flutter(飞镖)方式:
最小的可重现代码:
void main() {
WidgetsFlutterBinding.ensureInitialized();
MobileAds.instance.initialize();
runApp(MaterialApp(home: MyApp()));
}
class MyApp extends StatefulWidget {
@override
MyAppState createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
InterstitialAd.load(
adUnitId: 'ca-app-pub-3940256099942544/1033173712',
request: AdRequest(),
adLoadCallback: InterstitialAdLoadCallback(
onAdLoaded: (ad) => ad.show(),
onAdFailedToLoad: (e) => print('Failed: $e'),
),
);
}
@override
Widget build(BuildContext context) => Container();
}
问题(在颤振上):
测试广告出现了,但是当我尝试使用我自己的广告 ID 和应用 ID(即使在发布模式下)时,它会因为这个错误而失败。
没有广告配置。
距离我在 Admob 上创建该广告单元已经一周了。我在 Play 商店中也有一个不同的应用程序(它正在显示广告),但即使我在代码中使用该应用程序的 ID,它也无法加载带有错误代码 3 的广告。
PS:已经尝试过这个解决方案,但没有奏效。
Android 或 Flutter 解决方案中的任何一个都适合我
解决方案
我记得,加载一个 Ad 对象后,您需要将其显示给用户,以便设备可以根据条件或事件等知道何时显示 Ad。
加载广告:
InterstitialAd.load(
adUnitId: '<ad unit id>',
request: AdRequest(),
adLoadCallback: InterstitialAdLoadCallback(
onAdLoaded: (InterstitialAd ad) {
// Keep a reference to the ad so you can show it later.
this._interstitialAd = ad;
},
onAdFailedToLoad: (LoadAdError error) {
print('InterstitialAd failed to load: $error');
},
));
监听广告状态的广告事件:
interstitialAd.fullScreenContentCallback = FullScreenContentCallback(
onAdShowedFullScreenContent: (InterstitialAd ad) =>
print('$ad onAdShowedFullScreenContent.'),
onAdDismissedFullScreenContent: (InterstitialAd ad) {
print('$ad onAdDismissedFullScreenContent.');
ad.dispose();
},
onAdFailedToShowFullScreenContent: (InterstitialAd ad, AdError error) {
print('$ad onAdFailedToShowFullScreenContent: $error');
ad.dispose();
},
onAdImpression: (InterstitialAd ad) => print('$ad impression occurred.'),
);
要显示广告:
myInterstitial.show();
正如我在您的代码中看到的,您没有在 UI 代码中显示插页式广告。如我错了请纠正我。
编辑:
如果您询问如何myInterstitial.show();
在代码中使用它,您可以在 FlatButton、TextButton、GestureDetector 等以及 onTap 或 onPressed 函数中使用它,使用myInterstitial.show();
推荐阅读
- dockerfile - 当应用程序部署在 Windows 容器上时,Dontnet Framework 应用程序的日志问题
- java - 使用函数显示作业列表
- python - 用于列/数组中所有可能的连续条目组合的 Python 循环
- python - 如何处理类中的 matplotlib 对象?
- python - matplotlib - 将 2D 数据转换为 3D 数据
- c# - Await 和 .Result 一直在等待
- python - 确定两个计数数组之间的相似性
- python - python plotly graph_objects box marker的属性异常颜色不起作用(可能的错误)
- java - 如何在 Spring websocket stomp 服务器中使用 ChannelInterceptorAdapter 断开客户端?
- coq - Coq:关联变量的归纳