Can't add ads to my app.

2020-04-05 08:52发布

So I followed this guide from admob with Google Play Services, https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals#play And I have encountered a problem. The code they provided on their website is not working.I get errors.

    package puske.com;

import com.google.android.gms.ads.*;
import com.google.ads.AdSize;
import com.google.ads.AdView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

public class Menu extends Activity {
private AdView adView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);
    // Create the adView.
    adView = new AdView(this);
    adView.setAdUnitId(MY_AD_UNIT_ID);
    adView.setAdSize(AdSize.BANNER);

    // Lookup your LinearLayout assuming it's been given
    // the attribute android:id="@+id/mainLayout".
    RelativeLayout layout = (RelativeLayout)findViewById(R.id.menuz);

    // Add the adView to it.
    layout.addView(adView);

    // Initiate a generic request.
    AdRequest adRequest = new AdRequest.Builder().build();

    // Load the adView with the ad request.
    adView.loadAd(adRequest);

I get error on adView = new AdView(this); the Constructor AdView(Menu) is undefined.

adView.setAdSize(AdSize.BANNER); the Method setAdSize is undefined for the type AdView

And adView.loadAd(adRequest); The method load.ad(adRequest) in the type AdView is not applicable for the arguments (AdRequest)

I added https://developers.google.com/mobile-ads-sdk/docs/?hl=en_US as said in guide, and still doesn't work. http://prntscr.com/2o6vo4

5条回答
放荡不羁爱自由
2楼-- · 2020-04-05 08:59

Use

AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
adView.loadAd(adRequestBuilder.build());

EDIT:

Remove

setContentView(R.layout.menu);

add behind loadAd()

setContentView(layout);
查看更多
做自己的国王
3楼-- · 2020-04-05 09:00
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

AND adding following into layout:

<com.google.ads.AdView
            android:id="@+id/ad"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="Your AddMob ID"
            ads:loadAdOnCreate="true"
            ads:testDevices="TEST_EMULATOR,TEST_DEVICE_ID_GOES_HERE" /> 

works

查看更多
Luminary・发光体
4楼-- · 2020-04-05 09:21

Remove any other ad sdk(in my case it was adfonic) from your project as there is a conflict and try. Happy Coding :)

查看更多
姐就是有狂的资本
5楼-- · 2020-04-05 09:22

You can use layout to show your add. Just put below code in your layout where ever you want into the screen.

     <com.google.ads.AdView
            android:id="@+id/ad"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="Your AddMob ID"
            ads:loadAdOnCreate="true"
            ads:testDevices="TEST_EMULATOR,TEST_DEVICE_ID_GOES_HERE" />  
    </RelativeLayout>

You should have the newest GoogleAdmobSdk.jar in you libs folder. This is working for me.

查看更多
甜甜的少女心
6楼-- · 2020-04-05 09:24

Try to add directly at the import area the following lines

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

If you are migrating to new way of use addMob don`t forget to remove the old library.

查看更多
登录 后发表回答