尝试添加广告时发生致命崩溃

时间:2014-03-07 18:56:56

标签: android sdk admob adview

我一直在努力使用新的Google广告系统,我使用的是AdMob。但无论我尝试什么,它都会崩溃。 过去几个小时我一直在搜索解决方案,但还没有任何工作。它会给出一个新的错误或相同的错误。

  • 我做了什么:我在我的Eclipse中添加了google-play-services-lib作为项目, 并将其添加为我项目中的库。
  • 我在我的主要布局中添加了一个Google广告视图:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
    <com.google.android.gms.ads.AdView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ads="http://schemas.android.com/apk/lib/com.google.android.gms.ads"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-4118138803752321/8641047294" />
    
    </LinearLayout>
    
  • 我在主要活动之后添加了这个:

    <activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
    
  • 然后我使用此代码进行了广告请求:

    @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        AdView adView = (AdView)this.findViewById(R.id.adView);
        adView.loadAd(new AdRequest.Builder().build());
      }
    

现在它正在抛出错误: http://i.stack.imgur.com/oll62.png

2 个答案:

答案 0 :(得分:0)

尝试添加

xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

答案 1 :(得分:0)

好像忘记了布局中的xmlns:ads属性

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:ads="http://schemas.android.com/apk/res-auto"
          .....^

更新

同样删除以下adview本身的 xmlns 属性,您只需要在布局元素中使用它们:

 <com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-411813xxxxxxxxxxxxxxxxx" />

另请参阅此处的文档:https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals

相关问题