无法向我的应用添加广告.

Sli*_* C. 6 android banner admob google-play-services

因此,我在浏览器中使用了Google Play服务,https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals#play ,我遇到了一个问题.他们在他们的网站上提供的代码不起作用.我得到错误.

    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);
Run Code Online (Sandbox Code Playgroud)

adView = new AdView(this);在构造函数AdView(Menu)上出现错误是未定义的.

adView.setAdSize(AdSize.BANNER); the Method setAdSize is undefined for the type AdView
Run Code Online (Sandbox Code Playgroud)

并且adView.loadAd(adRequest); AdView类型中的方法load.ad(adRequest)不适用于参数(AdRequest)

我在指南中添加了https://developers.google.com/mobile-ads-sdk/docs/?hl=en_US,但仍无效.http://prntscr.com/2o6vo4

Dee*_*gir 2

您可以使用布局来显示您的添加。只需将以下代码放入您的布局中您想要在屏幕上显示的位置即可。

     <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>
Run Code Online (Sandbox Code Playgroud)

您的 libs 文件夹中应该有最新的 GoogleAdmobSdk.jar。这对我有用。