AdMob Ad and ErrorCode cannot be resolved to a type

12,046

Solution 1

As per google admob new api services

Step:

  1. Download latest Google Play Services library
  2. BannerCodeActivity.java

    public class BannerCodeActivity extends Activity {
        private AdView mAdView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_banner_code_ad_listener);
    
            mAdView = new AdView(this);
            mAdView.setAdUnitId(getResources().getString(R.string.ad_unit_id));
            mAdView.setAdSize(AdSize.BANNER);
            mAdView.setAdListener(new ToastAdListener(this));
            RelativeLayout layout = (RelativeLayout) findViewById(R.id.mainLayout);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            layout.addView(mAdView, params);
            mAdView.loadAd(new AdRequest.Builder().build());
        }
    
        @Override
        protected void onPause() {
            mAdView.pause();
            super.onPause();
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            mAdView.resume();
        }
    
        @Override
        protected void onDestroy() {
            mAdView.destroy();
            super.onDestroy();
        }
    }
    
  3. manifest permission

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    
    <meta-data
                android:name="com.google.android.gms.version"
                android:value="@integer/google_play_services_version" />
     <activity
                android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
    

You have to follow all this thing more information from here.

if any trouble then let me know.

Solution 2

I presume you mean that you are getting a compilation failure. NB posting an actual error message is always a good idea on StackOverflow.

Check your imports. If you are using the Google Play Services library for Admob then the package has changed to com.google.android.gms.ads

The interface for com.google.android.gms.ads.AdListener is

public abstract class AdListener {
  public void onAdLoaded();
  public void onAdFailedToLoad(int errorCode);
  public void onAdOpened();
  public void onAdClosed();
  public void onAdLeftApplication();
}

NB this has changed since Admob-6.4.1 You appear to be using the AdListener from Admob-6.4.1

Solution 3

Almost everything you need to know how to migrate to the new API is in this Google Play Services Migration document.

One thing it doesn't mention though, are the error codes values (that now are int values) for the new onAdFailedToLoad function that replaces the old onFailedToReceiveAd. These are described in the AdListener reference page and in the AdRequest reference page

Share:
12,046
AndreaF
Author by

AndreaF

Updated on June 04, 2022

Comments

  • AndreaF
    AndreaF almost 2 years

    I have added this code to see the error if App fails to receive ads.

        // Create an ad request. Check logcat output for the hashed device ID to
        // get test ads on a physical device.
        AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
            .build();
    
        // Start loading the ad in the background.
        adView.loadAd(adRequest);
     // Set AdListener
        adView.setAdListener(new AdListener() {
            @Override
            public void onFailedToReceiveAd(Ad ad, ErrorCode error) {
                System.err.println("Ad failed: " + ad.toString() + error.toString());    
            }
    
            @Override
            public void onReceiveAd(Ad ad) {
                System.out.println("Ad received: " + ad.toString());
            }
        });
      }
    

    Unfortunately doesn't work because cannot find Ad and ErrorCode probably because in latest library have changed something.

    How could I fix this?

    • Zubair Ahmed
      Zubair Ahmed over 10 years
      have you entered this line <activity android:name="com.google.ads.AdActivity"</activity> in your Manifest?
    • AndreaF
      AndreaF over 10 years
      Yes, but the error persist
    • Harshid
      Harshid over 10 years
      can you try my answer.
  • AndreaF
    AndreaF over 10 years
    I use latest google-play-services.jar and the import are import com.google.android.gms.ads.AdListener; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdSize; import com.google.android.gms.ads.AdView; but unfortunately the object Ad seems missing
  • William
    William over 10 years
    You appear to be using the AdListener from Admob-6.4.1. You need to be using the new AdListener which does not reference Ad or ErrorCode. See my updated answer.
  • AndreaF
    AndreaF over 10 years
    Can you give me more details? How to print the error using the onAdFailedToLoad() and the new API that have no Ad object?
  • William
    William over 10 years
    See developers.google.com/mobile-ads-sdk/docs/admob/intermediate‌​. The error code is one of 4 errors defined there. Ad never really was relevant and was pulled.
  • AndreaF
    AndreaF over 10 years
    ok, but is unclear how to get the Ad info in my class since doesn't return anything and have no ad object where I can get the info with a toString. could you give me a sample? thanks
  • William
    William over 10 years
    There is no Ad object anymore. I suggest you review your use case. Why do you need it?
  • IgorGanapolsky
    IgorGanapolsky over 10 years
    @AndreaF Please review Google Play Services Migration for AdMob (developers.google.com/mobile-ads-sdk/docs/admob/play-migrat‌​ion). Then you will understand what people are talking about here when they're trying to help you.
  • Zar E Ahmer
    Zar E Ahmer over 8 years
    Zubair Ahmad Khan hmm.mm