“第一次布局后无法获取viewWidth”错误

时间:2011-09-15 19:21:36

标签: android xml admob

我不能为我的生活修复这个错误。我看过Google和周围没有解决方案。我的广告似乎在Android 1.5和1.6上工作正常,但在版本2以上我得到了这个错误。

我已将广告放在LinearLayout中,这是滚动视图中的第一个布局。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
  android:orientation="vertical"
  android:background="@drawable/bg_wallpaper"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">

    <LinearLayout 
        android:orientation="vertical" 
        android:id="@+id/drillactivity"
        android:layout_width="fill_parent" 
        android:layout_gravity="bottom" 
        android:layout_height="340dp">

             <com.google.ads.AdView
                android:id="@+id/adView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                ads:adUnitId="my id goes here"
                ads:adSize="BANNER"
                ads:loadAdOnCreate="true"/>

我没有在班上使用任何代码来投放广告。我更喜欢通过xml来做。

3 个答案:

答案 0 :(得分:3)

我有黑屏的类似警告。问题是设备上有两个不同的应用程序具有相同的AdMob ID。

我删除了第二个应用程序(错误地使用相同的ID)并且问题消失了。

答案 1 :(得分:3)

使用以下代码,我修复了此错误。

<com.google.ads.AdView
android:id="@+id/adView"
android:layout_width="320dp"
android:layout_height="50dp"
android:gravity="center"
ads:adUnitId="my id goes here"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"/>

我没有任何具体原因导致此错误,可能并不适用于所有情况。

答案 2 :(得分:0)

在xml中声明一个线性/相对布局,您在活动中使用它。然后将活动设置为adView的侦听器。 喜欢:

googleAdView = new AdView(Activity,AdSize.IAB_BANNER, ADMOB_SITE_ID);
googleAdView.setAdListener(Activity);
com.google.ads.AdRequest request = new com.google.ads.AdRequest();
this.adMobLayout.removeAllViews();
this.adMobLayout.addView(googleAdView);
googleAdView.loadAd(request);

此处adMobLayout是您在xml布局中声明的布局。