Java 收到广告响应 Admob 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29833509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:39:33  来源:igfitidea点击:

There was a getting an ad response Admob Error

javaandroidadmob

提问by Karnak

I want to update one source code from old GoogleAds SDKto new Google Play Service Library, but there is a problem. Everytime I got this error:

我想将一个源代码从 old 更新GoogleAds SDK为 new Google Play Service Library,但是出现问题。每次我收到这个错误:

There was a problem getting an ad response.

获取广告响应时出现问题。

ErrorCode: 0

Failed to load ad:0

错误代码:0

无法加载广告:0

This is code from playactivty:

这是 playactivty 的代码:

public String BANNER_AD_UNIT_ID;

AdRequest adRequest;

private void LoadAds()
{
    LinearLayout layout = (LinearLayout) findViewById(R.id.adView);
    this.BANNER_AD_UNIT_ID = getResources().getString(R.string.admob_id);
    // Create the adView
    AdView adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId(BANNER_AD_UNIT_ID);

    // Add the adView to it
    layout.addView(adView);

    // Initiate a generic request to load it with an ad
    AdRequest adRequest = new AdRequest.Builder()
    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
    .build();
//  AdRequest.setTesting(true);
    adView.loadAd(adRequest);   
}

And from layout.xml

从 layout.xml

    </RelativeLayout>

  <com.google.android.gms.ads.AdView

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    ads:adSize="BANNER"
    ads:adUnitId="@string/admob_id">
</com.google.android.gms.ads.AdView>
<LinearLayout
        android:orientation="horizontal"

        android:id="@+id/adView" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

</LinearLayout>

What I was doing wrong there? Can not find issue, and trust me, I was read all SOF posts about that error :)

我在那里做错了什么?找不到问题,相信我,我已经阅读了所有关于该错误的 SOF 帖子:)

Thank you very much!

非常感谢!

Edit:

编辑:

Original code:

原始代码:

PlayActivity.java

PlayActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play);
    LoadConfigParams();
    LoadSharedPreferences();
    LoadResources();
    LoadListeners();        
    LoadStage(mCurStage);
    LoadAds();


}

    private void LoadAds()
{
    LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayoutAdmob);

    // Create the adView
    AdView adView = new AdView(this, AdSize.SMART_BANNER, getResources().getString(R.string.admob_id));

    // Add the adView to it
    layout.addView(adView);

    // Initiate a generic request to load it with an ad
    AdRequest request = new AdRequest();
    request.setTesting(true);
    adView.loadAd(request); 
}

activity_play.xml(only last LinearLayout code, above that is RelativeLayout).

activity_play.xml(只有最后一个 LinearLayout 代码,上面是 RelativeLayout)。

    <LinearLayout
        android:id="@+id/linearLayoutAdmob" android:layout_width="fill_parent"
        android:layout_height="wrap_content"></LinearLayout>

采纳答案by Elltz

There are two ways of creating an Admobadd by javaor xml, the easiest way-(even though both are easy) is xml.Also if you use SMART_BANNERyou are going to get adds of different sizes, and if the size does not fit/match the remaining screen space yourRelativeLayouthas left for LinearLayoutyour add will not show. so i will assume you have taken care of that

有两种方法Admob可以通过javaor创建添加xml,最简单的方法(尽管两者都很简单)是xml。此外,如果您使用,SMART_BANNER您将获得不同尺寸的添加,并且如果尺寸不适合/匹配剩余的屏幕您RelativeLayoutLinearLayout添加留下的空间将不会显示。所以我假设你已经解决了这个问题

<LinearLayout
    android:id="@+id/linearLayoutAdmob" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

     <com.google.android.gms.ads.AdView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      ads:adSize="SMART_BANNER"
      android:id="@+id/myaddview"
      ads:adUnitId="@string/admob_id">
</LinearLayout>

then in your loadAds()

然后在你的 loadAds()

AdView adView = findViewById(R.id.myaddview); //add the cast
 AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
adView.loadAd(adRequest);   

thats all you need check thisto know what to import and double check your permissions

这就是所有你需要检查这个知道要导入的内容,并仔细检查您的权限

回答by RuslanAndroid

MobileAds.initialize(this);

MobileAds.initialize(this);

works for me

为我工作