0

I'm trying to insert AdMob ads into my android app and whatever I do it just won't work. I tried adding them in XML and Java and in both cases the app crashes as soon as I start it up.

This is how I tried doing it in java (I got this from someone else's question here on stackoverflow):

//create an adView
RelativeLayout layout = (RelativeLayout)findViewById(R.id.relativelayout);
String pubID = "a151ba33e65bb39";
AdView adView = new AdView(this, AdSize.BANNER, pubID);
layout.addView(adView);
AdRequest request = new AdRequest();
//request.setTesting(true);
adView.loadAd(request);

And this is how I tried doing it in XML:

<ScrollView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background1"
android:fillViewport="true">

<RelativeLayout
android:id="@+id/relativelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

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

Here's my manifest XML file (only copied the part of AndroidManifest XML that I made for ads):

<activity 
        android:name="com.google.ads.AdActivity" 
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
</application>

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

</manifest>

In both cases the app crashes as soon as I try to compile it to my phone.

Logcat file:

06-17 15:04:11.347: E/AndroidRuntime(5662): FATAL EXCEPTION: main
06-17 15:04:11.347: E/AndroidRuntime(5662): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.matthewstudios.gw2legendary/com.example.gw2legendary.MainActivity}: android.view.InflateException: Binary XML file line #20: Error inflating class com.google.ads.AdView
Guy
  • 6,414
  • 19
  • 66
  • 136
  • please post the logcat output – Opiatefuchs Jun 17 '13 at 12:59
  • Not sure which part of logcat to post, I hope i did the right one tho (check my original post edit) – Guy Jun 17 '13 at 13:07
  • 2
    it seems that You have done something wrong with adding the adMob jar to Your project....take a look at here: http://stackoverflow.com/questions/15550718/android-app-fail-to-start-after-adding-admob/15551286#15551286 – Opiatefuchs Jun 17 '13 at 13:10
  • thank you so much! I think adding the admob jar wasn't the problem. I moved permissions in manifest XML inside the and it worked. Thanks for giving me the link :) – Guy Jun 17 '13 at 13:25
  • no problem, so if that link was helpful, please rate it – Opiatefuchs Jun 17 '13 at 13:28

1 Answers1

0

I don't want to leave the question unanswered so I'll just link to the awesome answer that helped me solve the problem!

Follow these stepes and adMob will work 100%!

Community
  • 1
  • 1
Guy
  • 6,414
  • 19
  • 66
  • 136