有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在我的Android应用程序中添加Admob广告?

我已经读了这篇文章,现在我有了代码

package com.nda.admob;

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

import 安卓.app.Activity;
import 安卓.os.Bundle;
import 安卓.util.AttributeSet;
import 安卓.widget.LinearLayout;

public class AdMobTestingActivity extends Activity {
    /** Called when the activity is first created. */
    AdView adView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        adView = new AdView(this, AdSize.BANNER,  "a14eb6c98335a35");
        LinearLayout l=(LinearLayout)findViewById(R.id.linear);
        l.addView(adView);

        AdRequest request = new AdRequest();
        adView.loadAd(request);
    }
}

以及AndroidManifest的代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
      package="com.nda.admob"
      安卓:versionCode="1"
      安卓:versionName="1.0">
    <application 安卓:icon="@drawable/icon" 安卓:label="@string/app_name">
        <activity 安卓:name=".AdMobTestingActivity"
                  安卓:label="@string/app_name">
            <intent-filter>
                <action 安卓:name="安卓.intent.action.MAIN" />
                <category 安卓:name="安卓.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity 安卓:name="com.google.ads.AdActivity" 
              安卓:configChanges="keyboard|keyboardHidden|orientation"/>       
    </application>
        <uses-sdk 安卓:minSdkVersion="3" />
    <uses-permission 安卓:name="安卓.permission.INTERNET"/>
    <uses-permission 安卓:name="安卓.permission.ACCESS_NETWORK_STATE"/>
</manifest>

但我始终有这样一条信息:“您必须在AndroidManifest.xml中声明AdActivity,并进行配置更改”。我哪里出错了


共 (1) 个答案

  1. # 1 楼答案

    如果您使用的是最新的admob sdk,则需要如下定义您的AdActivity:

        <activity android:name="com.google.ads.AdActivity"
              android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
    

    请注意,一些configChanges选项需要更高版本的Android SDK(API 11级),正如您提供的链接中所述,您需要:

    Requirements

    The Google AdMob Ads SDK for Android requires Android 1.5 or later. Make sure you have the latest copy of the Android SDK and that you're compiling against at least Android v3.2 (set target in default.properties to android-13).

    希望能有所帮助