2013-01-15 183 views
0

可能重複:
admob 6.2.1 nullpointer exception安卓顯示java.lang.NullPointerException錯誤

,如果我想一個廣告橫幅添加到我的佈局,該錯誤只顯示了。

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" 
     /> 

我無法修復錯誤,希望有更多經驗的人可以指出問題所在。謝謝!

+0

如何添加將Api佈局設置爲10或更低?正如他的回答中所述。我想看看這是否會解決它。 – Questions

+0

我不確定這到底是什麼意思。你可能想問他。 – Geobits

+0

6個月前被問了! – Questions

回答

1

我面對這一問題,然後我刪除從XML文件AD瀏覽佈局,並放置有一個簡單的線性佈局,並通過代碼在我activity.It動態附AD瀏覽解決problem.This代碼可以幫助你 -

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()); 
相關問題