11

I'm incorporating AdMob into my app. I've followed the steps in the developers page. However AdRequest.Builder() is being underlined with red and it says:

AdRequest cannot be resolved to a type

and

AdRequest.Builder cannot be resolved to a type.

What could be the problem?

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


public class FireRoomActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fire_room);

        // Look up the AdView as a resource and load a request.
        AdView adView = (AdView)this.findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();   
        adView.loadAd(adRequest);
    }

In xml I've shown admob as such:

<com.google.android.gms.ads.AdView android:id="@+id/adView"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     ads:adUnitId="bla bla"
                     ads:adSize="BANNER"/>
Nazerke
  • 2,098
  • 7
  • 37
  • 57

3 Answers3

35

Your code is mix for Admob SDK (Google Play) and Android (6.4.1 and earlier SDKs)

Use

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

AdRequest adRequest = new AdRequest.Builder().build();

and

<com.google.android.gms.ads.AdView android:id="@+id/adView"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     ads:adUnitId="bla bla"
                     ads:adSize="BANNER"/>

if you use Admob SDK (Google Play)

Or use

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

AdRequest adRequest = new AdRequest();

and

<com.google.ads.AdView android:id="@+id/adView"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     ads:adUnitId="bla bla"
                     ads:adSize="BANNER"/>

if you use earlies SDK

For Admob SDK (Google Play) not forget to change namespase

xmlns:ads="http://schemas.android.com/apk/res-auto"
itvdonsk
  • 561
  • 4
  • 6
4

Try this..

AdRequest adRequest = new AdRequest();   
AdView adView = (AdView)this.findViewById(R.id.adView);           
adView.loadAd(adRequest);

Note:

  1. Make sure you have included the necessary library in your project and permissions in your manifest.

  2. Also check whether you have given correct ad-mob id in your xml.

EDIT :

To add test device, you could try

adRequest.addTestDevice(AdRequest.TEST_EMULATOR);  //for emulators
adRequest.addTestDevice("device_id");              //for real device, enter device id
Hariharan
  • 24,741
  • 6
  • 50
  • 54
  • it of course works. But I'll need later to test on the phone like: AdRequest request = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // All emulators .addTestDevice("AC98C820A50B4AD8A2106EDE96FB87D4") // My Galaxy Nexus test phone .build(); But it will give me an error. – Nazerke Dec 16 '13 at 09:56
  • Generally, the documentation says: "Before being passed to AdView.loadAd, an AdRequest may be customized using an AdRequest.Builder to allow Google to better target ads. " In the xml I did this, which is just copy paste from the documentation: – Nazerke Dec 16 '13 at 09:57
  • I've included library, otherwise the rest of the code would be underlined. And yes I've added network permissions in the AndroidManifest – Nazerke Dec 16 '13 at 09:59
  • @Nazerke Even if you run this code alone, you will get test ads in emulator and testing device. once if the app is uploaded in playstore the app will receive real ads. – Hariharan Dec 16 '13 at 10:01
  • @Nazerke check my edit.. add those lines before `adView.loadAd(adRequest)` – Hariharan Dec 16 '13 at 10:12
  • Thanks, it is a workaround. I'm giving an upvote for a workaround, can't accept it as an answer because it doesn't answer why I'm getting the exception. – Nazerke Dec 16 '13 at 10:17
  • @Nazerke k no probz:) – Hariharan Dec 16 '13 at 10:22
0

Probably you are encountering Library Reference Bug of Eclips. Which returns this Types of error on following developers page's Steps.

Go To your Project Properties and Click on Android Tab. enter image description here

Check, if a Redcross mark exists.

if yes, you are encountering it for sure.

Eclipse does weird things when importing an existing project (google-play-services-lib), especially if you try to import and then allow the project to be automatically 'copied' to your workspace.

To Solve this,

  1. Erase all google-play-services projects from your workspace.
  2. Close Eclipse.
  3. Manually copy the google-play-services-lib folder (....sdk\extras\google\google_play_services\libproject\google-play-services_lib) into your workspace.
  4. Open Eclipse

Now add the Library From your workspace as existing project instead of

....sdk\extras\google\google_play_services\libproject\google-play-services_lib

At last, add project reference library from project properties as you did before.

Remember, directions on developers page is fully perfect without this single bug. So, Follow All other directions As the Page says.

Community
  • 1
  • 1
S.Ahsan
  • 389
  • 3
  • 15