首页 > 解决方案 > 即使使用测试广告单元,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 解决方案中的任何一个都适合我

标签: androidflutteradmobgooglemobileads

解决方案


我记得,加载一个 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();


推荐阅读