可能重复:
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"
/>
我无法修复错误,我希望有更多经验的人能指出问题。谢谢!