AdRequest.Builder cannot be resolved to a type

2020-08-13 04:24发布

问题:

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"/>

回答1:

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"


回答2:

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


回答3:

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.

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.



标签: android admob