【问题标题】:AdMob Ads not displaying on Android StudioAdMob 广告未在 Android Studio 上显示
【发布时间】:2018-01-13 03:42:47
【问题描述】:

我在将 admob 广告添加到 Android 应用程序时遇到了困难,并且不断收到错误消息:There was a problem getting an ad response. ErrorCode: 0 后跟 Failed to load ad: 0.,所以我直接从教程中复制了代码,但仍然遇到相同的错误。我在本教程中使用 android studio 提供的所有内容:https://developers.google.com/admob/android/quick-start

这里是xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="michaelsinn.contact.gmail.com.testads.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>

</android.support.constraint.ConstraintLayout>

和活动:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;

public class MainActivity extends AppCompatActivity {
    private AdView mAdView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MobileAds.initialize(this,"ca-app-pub-3940256099942544~3347511713");

        mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().addTestDevice("[Correct test device id is here]").build();
        mAdView.loadAd(adRequest);
    }
}

感谢您的任何帮助!谢谢

【问题讨论】:

标签: android admob


【解决方案1】:

试试这个。

AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
mAdView.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded() {
Toast.makeText(getApplicationContext(),"Ad Loaded",Toast.LENGTH_SHORT).show();
        // Code to be executed when an ad finishes loading.
    }

    @Override
    public void onAdFailedToLoad(int errorCode) {
Toast.makeText(getApplicationContext(),"Ad Failed to load",Toast.LENGTH_SHORT).show();
        // Code to be executed when an ad request fails.
    }

    @Override
    public void onAdOpened() {
Toast.makeText(getApplicationContext(),"Ad Opened",Toast.LENGTH_SHORT).show();
        // Code to be executed when an ad opens an overlay that
        // covers the screen.
    }

    @Override
    public void onAdLeftApplication() {
Toast.makeText(getApplicationContext(),"Ad Left Application",Toast.LENGTH_SHORT).show();
        // Code to be executed when the user has left the app.
    }

    @Override
    public void onAdClosed() {
Toast.makeText(getApplicationContext(),"Ad Closed",Toast.LENGTH_SHORT).show();
        // Code to be executed when when the user is about to return
        // to the app after tapping on an ad.
    }
});

【讨论】:

  • 我添加了代码,收到“广告加载失败”的输出
【解决方案2】:

你的代码很好

尝试在模拟器上运行应用并查看结果

另一件事

去admob

设置

测试设备

添加测试设备

然后按照说明添加您自己的设备作为测试设备

如果该广告与 测试广告 横幅效果良好,那么您就可以了

如果没有,请检查您的主宴

并确保您添加了

<meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-xxxxxxx~xxxxx" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    相关资源
    最近更新 更多