我正在尝试向我的应用添加广告,但我不知道我是如何按照此网站的说明进行的:
https://developer.android.com/google/play-services/ads.html
我在入门列中执行了第1步和第2步,我导入了google-play-services_libs 到我的工作区,然后我在我的应用程序中引用它,并在主要节点中添加了此代码:
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
我打开了示例广告并将此代码复制到我的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:gravity="center"
android:orientation="vertical" >
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
ads:adUnitId="@string/ad_unit_id"/>
但是我收到了这个错误:
The following classes could not be instantiated:
- com.google.android.gms.ads.AdView
我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.StarSurge.khwallpaper"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21"/>
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>
</manifest>
我的Java:
private AdView adView;
private static final String AD_UNIT_ID = "-omitted-";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.adBanner);
layout.addView(adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("-omitted-")
.build();
adView.loadAd(adRequest);
所以你知道如何解决这个问题,你能告诉我我应该在java中写些什么来感谢
答案 0 :(得分:0)
您是否在清单中添加了这个?
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
在java文件中:
AdView adView = (AdView) this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
// Add a test device to show Test Ads..comment for real app when launching
// .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
//.addTestDevice("CC5F2C72DF2B356BBF0DA198") //test this in emulator
.build();
// Load ads into Banner Ads
adView.loadAd(adRequest);