2

i am using this code which looks perfect without any error bt dont know why my application gets crashed everytime i run my app..

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

in android manifest.xml

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

in

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

logcat shows

04-12 21:30:09.655: E/dalvikvm(272): Could not find class 'com.google.ads.AdView', 
referenced from method com.project.hisaabkikitaab.MainActivity.onCreate
04-12 21:30:09.655: W/dalvikvm(272): VFY: unable to resolve check-cast 471 (Lcom/google
/ads/AdView;) in Lcom/project/hisaabkikitaab/MainActivity;
04-12 21:30:09.655: D/dalvikvm(272): VFY: replacing opcode 0x1f at 0x000e
04-12 21:30:09.655: D/dalvikvm(272): VFY: dead code 0x0010-02b9 in Lcom/project  
/hisaabkikitaab/MainActivity;.onCreate (Landroid/os/Bundle;)V
04-12 21:30:09.815: D/AndroidRuntime(272): Shutting down VM
04-12 21:30:09.815: W/dalvikvm(272): threadid=1: thread exiting with uncaught     
exception  (group=0x4001d800)
04-12 21:30:09.845: E/AndroidRuntime(272): FATAL EXCEPTION: main
04-12 21:30:09.845: E/AndroidRuntime(272): java.lang.NoClassDefFoundError:    
com.google.ads.AdView
04-12 21:30:09.845: E/AndroidRuntime(272):  at    
com.project.hisaabkikitaab.MainActivity.onCreate(MainActivity.java:42)
04-12 21:30:09.845: E/AndroidRuntime(272):  at   
.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

3 Answers 3

3

You need to add the ads namespace to the layout root node, too

xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"

The tutorial where you probably took the example from already has it:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
  <com.google.ads.AdView android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         ads:adUnitId="MY_AD_UNIT_ID"
                         ads:adSize="BANNER"
                         ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
                         ads:loadAdOnCreate="true"/>
</LinearLayout>

Source: https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals, Android tab

Another possible solution:

Is there a libs folder in your project? If so, is there the admob library? In case you say no to any of those, make sure a libs folder is created (at the same level of res and src, and copy your admob library there. Clean project and rebuild.

Sign up to request clarification or add additional context in comments.

5 Comments

i have already added that :( <LinearLayout xmlns:android="schemas.android.com/apk/res/android" xmlns:ads="schemas.android.com/apk/lib/com.google.ads" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/backmage" android:orientation="vertical" >
i have written my code through that developers page you are reffering bt dont know what is happening my app gets crash everytime i start .. is there is anything more that i am missing??
@scripter Try this: is there a libs folder in your project? If so, is there the admob library? In case you say no to any of those, make sure a libs folder is created (at the same level of res and src, and copy your admob library there. Clean project and rebuild.
yes i have libs folder bt not that admob library just one more 1 thing how to paste that admob library???
thanks man finally i added admob in libs and it's working great :)
0

Reference:

https://developers.google.com/admob/android/banner

build.gradle (app)

implementation 'com.google.android.gms:play-services-ads:20.1.0'

AndroidManifest.xml

<application
    .....
   <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
</application>

activity_main.xml

<com.google.android.gms.ads.AdView
  xmlns:ads="http://schemas.android.com/apk/res-auto"
  android:id="@+id/adView"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerHorizontal="true"
  android:layout_alignParentBottom="true"
  ads:adSize="BANNER"
  ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>

In MainActivity

private AdView mAdView;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MobileAds.initialize(this, new OnInitializationCompleteListener() {
        @Override
        public void onInitializationComplete(InitializationStatus initializationStatus) {
        }
    });

    mAdView = findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

    mAdView.setAdListener(new AdListener() {

    @Override
    public void onAdLoaded() {
    
    }

    @Override
    public void onAdFailedToLoad(LoadAdError adError) {
   
    }

    @Override
    public void onAdOpened() {
    
    }

    @Override
    public void onAdClicked() {

    }

    @Override
    public void onAdClosed() {
    
    }
  });
}

Comments

0

Sign in to your AdSense account. On the AdSense home page, click Add site. Enter the URL of the website where you want to show ads. Blogger, YouTube or other hosting partner. ... Click Save. Note: If you own multiple sites, you can also add them now.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.