There was a getting an ad response Admob Error

旧街凉风 提交于 2019-12-12 10:54:35

问题


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

There was a problem getting an ad response.

ErrorCode: 0

Failed to load ad:0

This is code from 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

    </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 :)

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).

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


回答1:


There are two ways of creating an Admob add by javaor 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 yourRelativeLayout has left for LinearLayout your add will not show. 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()

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




回答2:


MobileAds.initialize(this);

works for me



来源:https://stackoverflow.com/questions/29833509/there-was-a-getting-an-ad-response-admob-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!