我按照本教程了解了如何添加AdView
https://developers.google.com/mobile-ads-sdk/docs/
将此添加到我的AndroidManifest.xml
:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|sm allestScreenSize"/>
这是build.gradle
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile 'com.google.android.gms:play-services:4.0.30'
}
到我的java文件:
import com.google.android.gms.ads.*;
public class admob extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.admoblayout);
// Create an ad.
AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("My_UnitID_Here");
RelativeLayout layout = (RelativeLayout) findViewById(R.layout.admoblayout);
layout.addView(adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
}
但每当我打开活动时它都会崩溃
logcat:
FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: com.google.android.gms.ads.AdView
答案 0 :(得分:0)
有时,在添加新布局甚至修改现有布局时,IDE需要刷新项目才能生效。
刷新/重建项目后,您应该好好去。