我正在尝试将AdMob广告插入到我的Android应用中,无论我做什么,都无法正常工作。我尝试在XML和Java中添加它们,在这两种情况下,应用程序一启动就会崩溃。
这就是我在java中尝试的方法(我在stackoverflow上从别人的问题中得到了这个):
//create an adView
RelativeLayout layout = (RelativeLayout)findViewById(R.id.relativelayout);
String pubID = "a151ba33e65bb39";
AdView adView = new AdView(this, AdSize.BANNER, pubID);
layout.addView(adView);
AdRequest request = new AdRequest();
//request.setTesting(true);
adView.loadAd(request);
这就是我尝试用XML做的方法:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background1"
android:fillViewport="true">
<RelativeLayout
android:id="@+id/relativelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<com.google.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="a151ba33e65bb39"
ads:loadAdOnCreate="true" >
</com.google.ads.AdView>
这是我的清单XML文件(仅复制了我为广告制作的AndroidManifest XML部分):
<activity
android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
在这两种情况下,只要我尝试将其编译到手机上,应用就会崩溃。
Logcat文件:
06-17 15:04:11.347: E/AndroidRuntime(5662): FATAL EXCEPTION: main
06-17 15:04:11.347: E/AndroidRuntime(5662): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.matthewstudios.gw2legendary/com.example.gw2legendary.MainActivity}: android.view.InflateException: Binary XML file line #20: Error inflating class com.google.ads.AdView