0

I followed this tutorial on how to add AdView

https://developers.google.com/mobile-ads-sdk/docs/

By adding this to my AndroidManifest.xml :

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

<meta-data android:name="com.google.android.gms.version"
 android:value="@integer/google_play_services_version"/>

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

And this to build.gradle

dependencies {
compile 'com.android.support:appcompat-v7:+'
compile 'com.google.android.gms:play-services:4.0.30'
}

And to my java file:

import com.google.android.gms.ads.*;


public class admob extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.admoblayout);


    // Create an ad.
    AdView adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId("My_UnitID_Here");

    RelativeLayout layout = (RelativeLayout) findViewById(R.layout.admoblayout);
    layout.addView(adView);


    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);
}

}

But whenever I open the activity it crashes

logcat:

FATAL EXCEPTION: main java.lang.NoClassDefFoundError: com.google.android.gms.ads.AdView

user2989698
  • 81
  • 1
  • 10
  • Please provide the entire manifest.xml file. You have to ensure you place the codes accordingly or otherwise it won't work :) – SearchForKnowledge Mar 12 '14 at 21:10
  • Your manifest file and the code seem ok to me. Are you sure you've imported the google play lib into your project? – nKn Mar 12 '14 at 21:12
  • @nKn No I didn't import it, How can I import it using Android Studio?, I intalled google play services and google admob ads sdk but I didn't do anything except the ones I mentioned. – user2989698 Mar 12 '14 at 21:16
  • @SearchForKnowledge I'm sure I pasted it in the right place. Just like the tutorial I followed – user2989698 Mar 12 '14 at 21:17
  • 1
    Have a look at this, seems helpful: http://stackoverflow.com/questions/21081598/import-google-play-services-library-in-android-studio – nKn Mar 12 '14 at 21:18
  • Hmmmmmmmmmmm... that is weird than. Have you dont a project clean up? – SearchForKnowledge Mar 12 '14 at 21:20
  • @nKn i didn't get it ... I checked Project Structure and I found the library what should i do then? how to import it? or is it already imported? – user2989698 Mar 12 '14 at 21:41
  • @SearchForKnowledge Yeah I did. I guess it's because i didn't import the google play library – user2989698 Mar 12 '14 at 22:01
  • @SearchForKnowledge No I don't know how to import it in Android Studio + When I add the AdView to my layout it shows "Ads by Google" doesn't mean that everything is installed? – user2989698 Mar 13 '14 at 19:03
  • The first takes a little while but after some time you should see the ADS. When it says Ads by Google, is it like a black box or a white box that appears along with it? – SearchForKnowledge Mar 13 '14 at 19:51
  • @SearchForKnowledge A white one. – user2989698 Mar 13 '14 at 20:03
  • Can you post a screenshot? If it's white and it's blocking your background the ads are loading. Just sometime takes a while for it show :) – SearchForKnowledge Mar 13 '14 at 20:28
  • @SearchForKnowledge I rebuild my project and it worked fine, Thank you !! – user2989698 Mar 13 '14 at 21:42

1 Answers1

0

Sometime when adding new layouts or even modifying an existing layout, IDE requires the refresh of the project for it to take affect.

Once you refresh/rebuild the project you should be good to go.

SearchForKnowledge
  • 3,663
  • 9
  • 49
  • 122