Shophia Rajan
Shophia Rajan

Reputation: 11

Error in calling adView programmatically?

I am calling the Admob ad in my app programmatically. First I am declaring the imports

import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdSize
import com.google.android.gms.ads.AdView

Then defining my adid as string

String my_ad_unit_id="*some number*";

calling the ad as -

this.adView=new AdView (this,AdSize.BANNER,this.my_ad_unit_id);
((LinearLayout) this.findViewById(R.id.main)).addView(this.adView);
this.adView.loadAd(new AdRequest());

There is error only in the calling part. The errors are -

Upvotes: 1

Views: 2054

Answers (1)

Nana Ghartey
Nana Ghartey

Reputation: 7927

You've imported classes from the google play services library but you're using the older admob api.

If you're using admob via the google play services library, make the following changes to your code:

     adView = new AdView(this);
     adView.setAdUnitId(); 
     adView.setAdSize(AdSize.BANNER);
     AdRequest adRequest = new AdRequest.Builder().build();
     adView.loadAd(adRequest);

If you are using the old GoogleAdMobAdsSdk-6.4.1.jar, make sure you import from the right packages:

import com.google.ads.Ad;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

Upvotes: 6

Related Questions