Android 如何使用AdMob?
我试图用AdMob做的一切都彻底失败了,我似乎不太明白为什么。如果我试图将谷歌提供的活动作为我的主要活动,应用程序就会崩溃。如果我让它成为自己的班级。广告就这么出现了,我甚至不知道如何正确地为AdMob生成XML。下面是我的AdMob活动,如果您想查看:Android 如何使用AdMob?,android,Android,我试图用AdMob做的一切都彻底失败了,我似乎不太明白为什么。如果我试图将谷歌提供的活动作为我的主要活动,应用程序就会崩溃。如果我让它成为自己的班级。广告就这么出现了,我甚至不知道如何正确地为AdMob生成XML。下面是我的AdMob活动,如果您想查看: package com.dogger20011.mcpemodlocater; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.Ad
package com.dogger20011.mcpemodlocater;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
/**
* A simple {@link Activity} that embeds an AdView.
*/
public class AdMob extends Activity {
/** The view to show the ad. */
private AdView adView;
/* Your ad unit id. Replace with your actual ad unit id. */
private static final String AD_UNIT_ID = "INSERT_YOUR_AD_UNIT_ID_HERE";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page);
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
layout.addView(adView);
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
}
@Override
public void onResume() {
super.onResume();
if (adView != null) {
adView.resume();
}
}
@Override
public void onPause() {
if (adView != null) {
adView.pause();
}
super.onPause();
}
/** Called before the activity is destroyed. */
@Override
public void onDestroy() {
// Destroy the AdView.
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
}
我按照此教程将ADMOB集成到我的应用程序中 我的Xml代码如下所示
<com.google.ads.AdView android:id="@+id/ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="xxxxxx-xxxx-xxxx-xx"
ads:testDevices="TEST_EMULATOR,TEST_DEVICE_ID_GOES_HERE"
ads:loadAdOnCreate="true"
/>
它非常适合我当我有了XML时,它会使我的应用程序崩溃。当我去预览时,它说找不到以下类:com.google.Ads.advie,我将google Play Services添加为一个库,并将Play Services.jar复制到我的lib folderI中。我最初也遇到了同样的情况。然后你必须在你的libs文件夹中添加GoogleedMobadsdk-6.4.1.jar……这将解决问题。我做到了,现在它说无法实例化以下类:-com.google.ads.AdView当单击Open Class时,它说找不到AdView.Class的源代码。您是否在java构建路径中添加了GoogleAddMobaDSDK-6.4.1.jar??