Admob Error appeared

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

Failed to get ad response.

ErrorCode: 0

Failed to load ad: 0

This is the 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 did I do there? I can not find the problem and trust me, I read all the SOF messages about this error :)

Many thanks!

Edit:

Source:

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 the latest LinearLayout code, above RelativeLayout).

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

+5
source share
2 answers

There are two ways to create Admob add java or xml , the easiest way (although both are lightweight) is xml . Also, if you use SMART_BANNER , you are going to receive additions of different sizes, and if the size does not match / matches the remaining screen of your RelativeLayout for LinearLayout , your addition will not be displayed. so I guess you took 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 loadAds()

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

all you need to check this one is to find out what you need to import and double check your rights.

+3
source

MobileAds.initialize (this);

works for me

0
source

All Articles