0

Possible Duplicate:
admob 6.2.1 nullpointer exception

This error only shows up if I want to add an Ad banner to my layout.

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)

This is my xml file code.

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

If I remove this code that is responsible for showing the Ads from my xml file, the error disappears.

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

I can't fix the error, I hope someone with more experience can point the problem. Thank you!

Community
  • 1
  • 1
Questions
  • 303
  • 5
  • 16
  • How do I add set the Api layout to 10 or lower? As stated in his answer. I want to see if this would fix it. – Questions Jan 15 '13 at 02:48
  • I'm not sure exactly what that's supposed to mean. You might want to ask him. – Geobits Jan 15 '13 at 02:50
  • It was asked 6 months ago! – Questions Jan 15 '13 at 02:52
  • Less than 3, really, but that's beside the point. If you want to know how *he* fixed it, the best route is to ask *him*. – Geobits Jan 15 '13 at 02:54
  • Sorry, I missed read it. – Questions Jan 15 '13 at 02:57
  • There is no option to leave a comment, only to answer his question. – Questions Jan 15 '13 at 02:59
  • The upshot is you cannot use the graphical layout with `AdView`, even setting the API to 10 (pulled down with the Green Android Robot near the upper right), the NPE goes away, but it is traded for a handled expection that still makes the graphical editor useless. – iagreen Jan 15 '13 at 03:11
  • So if I set the API to 10 the NPE error will go away. Is this what you mean? Right now it is set to plateform 4.2, API level 17. – Questions Jan 15 '13 at 03:24

1 Answers1

1

I faced this problem, then i removed the adview layout from xml file and placed a simple linear layout there and attached adview dynamically through code in my activity.It solved the problem.This code can help you-

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());
Vikram Singh
  • 1,420
  • 14
  • 19