<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/libs/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/root">
<com.google.ads.AdView
android:id="@+id/ad"
android:layout_width="fill_parent"
android:layout_height="50dp"
ads:adSize="BANNER"
ads:adUnitId="a14b7042de197ce" />
</RelativeLayout>
错误:
01-21 23:40:31.462: E/AndroidRuntime(732): FATAL EXCEPTION: main
01-21 23:40:31.462: E/AndroidRuntime(732): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sampadintegration/com.example.sampadintegration.MainActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class com.google.ads.AdView
01-21 23:40:31.462: E/AndroidRuntime(732): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
01-21 23:40:31.462: E/AndroidRuntime(732): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-21 23:40:31.462: E/AndroidRuntime(732): at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-21 23:40:31.462: E/AndroidRuntime(732): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-21 23:40:31.462: E/AndroidRuntime(732): at android.os.Handler.dispatchMessage(Handler.java:99)
01-21 23:40:31.462: E/AndroidRuntime(732): at android.os.Looper.loop(Looper.java:137)
01-21 23:40:31.462: E/AndroidRuntime(732): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-21 23:40:31.462: E/AndroidRuntime(732): at java.lang.reflect.Method.invokeNative(Native Method)
01-21 23:40:31.462: E/AndroidRuntime(732): at java.lang.reflect.Method.invoke(Method.java:511)
01-21 23:40:31.462: E/AndroidRuntime(732): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-21 23:40:31.462: E/AndroidRuntime(732): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-21 23:40:31.462: E/AndroidRuntime(732): at dalvik.system.NativeStart.main(Native Method)
01-21 23:40:31.462: E/AndroidRuntime(732): Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class com.google.ads.AdView
01-21 23:40:31.462: E/AndroidRuntime(732): at android.view.LayoutInflater.createView(LayoutInflater.java:606)
01-21 23:40:31.462: E/AndroidRuntime(732): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
01-21 23:40:31.462: E/AndroidRuntime(732): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
01-21 23:40:31.462: E/AndroidRuntime(732): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-21 23:40:31.462: E/AndroidRuntime(732): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-21 23:40:31.462: E/AndroidRuntime(732): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-21 23:40:31.462: E/AndroidRuntime(732): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
01-21 23:40:31.462: E/AndroidRuntime(732): at android.app.Activity.setContentView(Activity.java:1835)
01-21 23:40:31.462: E/AndroidRuntime(732): at com.example.sampadintegration.MainActivity.onCreate(MainActivity.java:21)
01-21 23:40:31.462: E/AndroidRuntime(732): at android.app.Activity.performCreate(Activity.java:4465)
01-21 23:40:31.462: E/AndroidRuntime(732): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-21 23:40:31.462: E/AndroidRuntime(732): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
01-21 23:40:31.462: E/AndroidRuntime(732): ... 11 more
01-21 23:40:31.462: E/AndroidRuntime(732): Caused by: java.lang.reflect.InvocationTargetException
01-21 23:40:31.462: E/AndroidRuntime(732): at java.lang.reflect.Constructor.constructNative(Native Method)
01-21 23:40:31.462: E/AndroidRuntime(732): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
01-21 23:40:31.462: E/AndroidRuntime(732): at android.view.LayoutInflater.createView(LayoutInflater.java:586)
01-21 23:40:31.462: E/AndroidRuntime(732): ... 22 more
01-21 23:40:31.462: E/AndroidRuntime(732): Caused by: java.lang.RuntimeException: Could not initialize AdView: Required XML attribute "adSize" missing
01-21 23:40:31.462: E/AndroidRuntime(732): at com.google.ads.AdView.a(SourceFile:430)
01-21 23:40:31.462: E/AndroidRuntime(732): at com.google.ads.AdView.<init>(SourceFile:126)
01-21 23:40:31.462: E/AndroidRuntime(732): ... 25 more
01-21 23:40:31.462: E/AndroidRuntime(732): Caused by: com.google.ads.internal.b: Required XML attribute "adSize" missing
01-21 23:40:31.462: E/AndroidRuntime(732): at com.google.ads.AdView.b(SourceFile:495)
01-21 23:40:31.462: E/AndroidRuntime(732): at com.google.ads.AdView.a(SourceFile:364)
01-21 23:40:31.462: E/AndroidRuntime(732): ... 26 more
答案 0 :(得分:0)
请尝试使用此代码..
<强> XML 强>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/root">
<LinearLayout
android:id="@+id/ad"
android:layout_width="fill_parent"
android:layout_height="50dp"/>
</RelativeLayout>
<强> MainActivity 强>
AdView adView = new AdView(this, AdSize.BANNER, "a14b7042de197ce");
LinearLayout layout = (LinearLayout)findViewById(R.id.ad);
layout.addView(adView);
adView.loadAd(new AdRequest());