简体   繁体   English

有一个广告响应Admob错误

[英]There was a getting an ad response Admob Error

I want to update one source code from old GoogleAds SDK to new Google Play Service Library , but there is a problem. 我想将旧的GoogleAds SDK一个源代码更新为新的Google Play Service Library ,但是存在问题。 Everytime I got this error: 每次我收到此错误:

There was a problem getting an ad response. 获得广告响应时出现问题。

ErrorCode: 0 ErrorCode:0

Failed to load ad: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

@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>

There are two ways of creating an Admob add by java or xml , the easiest way-(even though both are easy) is xml .Also if you use SMART_BANNER you are going to get adds of different sizes, and if the size does not fit/match the remaining screen space your RelativeLayout has left for LinearLayout your add will not show. 有两种方法可以通过javaxml创建Admob添加,最简单的方法 - (即使两者都很简单)是xml 。如果你使用SMART_BANNER你会得到不同大小的添加,如果大小不合适/匹配您的RelativeLayoutLinearLayout留下的剩余屏幕空间,您的添加将不会显示。 so i will assume you have taken care of that 所以我会假设你已经照顾好了

<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 this to know what to import and double check your permissions 多数民众赞成你需要检查这一点以了解要导入的内容并仔细检查你的权限

MobileAds.initialize(this); MobileAds.initialize(本);

works for me 适合我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM