0

可能重复:
admob 6.2.1 空指针异常

仅当我想在布局中添加广告横幅时才会显示此错误。

java.lang.NullPointerException
Exception details are logged in Window > Show View > Error Log

java.lang.NullPointerException
    at com.google.ads.AdView.onMeasure(SourceFile:670)
    at android.view.View.measure(View.java:15513)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
    at android.view.View.measure(View.java:15513)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at android.view.View.measure(View.java:15513)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
    at android.view.View.measure(View.java:15513)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
    at android.view.View.measure(View.java:15513)

这是我的 xml 文件代码。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/bGoBack"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GO BACK" />

    <Button
        android:id="@+id/bRefresh"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="40dp"
        android:text="REFRESH" />

    <Button
        android:id="@+id/bForward"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="48dp"
        android:text="GO FORWARD" />

    <WebView
        android:id="@+id/webView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="40dp" />

    <com.google.ads.AdView android:id="@+id/adView" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="can't show my publisher id"
        ads:loadAdOnCreate="true"
    />


</LinearLayout>

如果我从我的 xml 文件中删除负责显示广告的代码,错误就会消失。

<com.google.ads.AdView android:id="@+id/adView" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ads:adSize="BANNER"
            ads:adUnitId="can't show my publisher id"
            ads:loadAdOnCreate="true"
        />

我无法修复错误,我希望有更多经验的人能指出问题。谢谢!

4

1 回答 1

1

我遇到了这个问题,然后我从 xml 文件中删除了 adview 布局,并在那里放置了一个简单的线性布局,并通过我的活动中的代码动态附加了 adview。它解决了这个问题。这段代码可以帮助你-

AdView adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID);
LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);//your linear layout
layout.addView(adView);// Add the adView to it
adView.loadAd(new AdRequest());
于 2013-01-15T04:37:24.273 回答