KickAss
KickAss

Reputation: 4290

Android AdMob crashes app on start

I don't what's wrong with this code but AdMob crashes the App. I made a new project with just AdMob in it and it still crashes. Any ideas?

I imported the AdMob .jar rightclick project > Properties > Java Build Path > add external JARs and added the GoogleAdMobAdsSdk...jar

layout xml:

<LinearLayout xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

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

main java:

package com.example.admob;

import android.app.Activity;
import android.os.Bundle;
import com.google.ads.AdRequest;
import com.google.ads.AdView;

public class MainActivity extends Activity {
AdView adView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    AdRequest adR = new AdRequest();
    adR.addTestDevice("4df1559f64826fed");
    adView = (AdView)findViewById(R.id.linearLayout);
    adView.loadAd(adR);

}

@Override
public void onDestroy() {
    if (adView != null) {
        adView.destroy();
    }
    super.onDestroy();
}

}

Please help :)

Upvotes: 0

Views: 2646

Answers (1)

Eric Leichtenschlag
Eric Leichtenschlag

Reputation: 8931

Your problem is:

adView = (AdView)findViewById(R.id.linearLayout);

Instead you want:

adView = (AdView)findViewById(R.id.adView);

to find the AdView instead of the LinearLayout. You're app is crashing because you're trying to cast a LinearLayout to an AdView.

I recommend you view the LogCat output from Android so you can see why it's complaining. It'll be a useful tool as you start working with Android. You can add it into Eclipse via:

Window -> Show View -> Other... -> Android/Logcat

Upvotes: 1

Related Questions