Abhijit
Abhijit

Reputation: 41

Android AdView NoClassDefFoundError

I'm a Beginner in Android Development. I just released an app but found some glitches, so I thought of correcting those glitches by creating a new Android project with the same package name as that of the released app. But once I finished with the update app, This app is not running on phone(debugging). Says "Unfortunately thisApp has Stopped." And also im trying to incorporate adMob this time.

Please help me because I have to publish this asap.

Here's the Logcat once the app crashes :

08-11 18:14:31.063: E/dalvikvm(15877): Could not find class 'com.google.ads.AdView', referenced from method com.gamerspitch.easybluetooth.BlueActivity.initAdView
08-11 18:14:31.254: E/AndroidRuntime(15877): FATAL EXCEPTION: main
08-11 18:14:31.254: E/AndroidRuntime(15877): java.lang.NoClassDefFoundError: com.google.ads.AdView
08-11 18:14:31.254: E/AndroidRuntime(15877):    at com.gamerspitch.easybluetooth.BlueActivity.initAdView(BlueActivity.java:107)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at com.gamerspitch.easybluetooth.BlueActivity.onCreate(BlueActivity.java:40)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at android.app.Activity.performCreate(Activity.java:5133)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at android.os.Looper.loop(Looper.java:137)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at android.app.ActivityThread.main(ActivityThread.java:5103)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at java.lang.reflect.Method.invokeNative(Native Method)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at java.lang.reflect.Method.invoke(Method.java:525)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-11 18:14:31.254: E/AndroidRuntime(15877):    at dalvik.system.NativeStart.main(Native Method)

Here is the XML of my admob placement. I just followed this link to Add admob.

<RelativeLayout 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="match_parent"
android:layout_height="match_parent"
android:background="@drawable/easyb"
tools:context=".BlueActivity" >

<LinearLayout
    android:id="@+id/adviewPlaceholder"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:orientation="vertical" >
</LinearLayout>

//Other elements

And I've put this in my manifest file >

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

    </activity>

And this in my Activity onCreate method >

private AdView ad;


@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_blue);

    initAdView();
//Other elements
protected void initAdView() {

    ad = new AdView(this, AdSize.SMART_BANNER, "a15204b9eb97566");

    LinearLayout ll = (LinearLayout)findViewById(R.id.adviewPlaceholder);

    ll.addView(ad);

    ad.loadAd(new AdRequest());
}

protected void destroyAdView() {
    if(ad != null) ad.destroy();
}

@Override
protected void onDestroy() {    
    // destroy the ad when the activity is destroyed
    destroyAdView();
    super.onDestroy();
}

Thanks in Advance

Upvotes: 0

Views: 357

Answers (3)

William
William

Reputation: 20196

There is nothing wrong with your code or your XML AFAICT. The error clearly states:

Could not find class 'com.google.ads.AdView'

It would seem that your deployed app does not contain the Admob library. You need to work out why. Check your build tool/environment.

Upvotes: 0

Philipp Jahoda
Philipp Jahoda

Reputation: 51431

According to this line in the error message:

08-11 02:28:56.973: E/AndroidRuntime(27461): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gamerspitch.easybluetooth/com.gamerspitch.easybluetooth.BlueActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class com.google.ads.AdView

There is a problem with your AdView that causes the app to crash.

Could you please post your .xml layout file as well as the Activity.

UPDATE:

In order to make this a bit more clear. I never define the AdView in .xml. I simply create a Layout inside my layout .xml file that has no children, and via code I add the AdView to it. It looks like this:

      private AdView ad;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);

          setContentView(R.layout.yourlayout);

          initAdView();

          // other code...
      }

       protected void initAdView() {

            ad = new AdView(this, AdSize.SMART_BANNER, "MY_AD_UNIT_ID");

            LinearLayout ll = (LinearLayout) findViewById(R.id.adviewPlaceholder);

            ll.addView(ad);

            ad.loadAd(new AdRequest());
        }

        protected void destroyAdView() {
            if(ad != null) ad.destroy();
        }

        @Override
        protected void onDestroy() {    
            // destroy the ad when the activity is destroyed
            destroyAdView();
            super.onDestroy();
        }

And the Layout yourlayout.xml file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent >

    <!-- lots of other layout stuff here -->


    <!-- make the adview be on the bottom of the screen -->
    <LinearLayout
        android:id="@+id/adviewPlaceholder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:orientation="vertical" >
    </LinearLayout>
 </RelativeLayout>

Upvotes: 2

wshelor
wshelor

Reputation: 383

It looks like your adView is causing the crash. How are you implementing it? Have you included all the permissions required in the manifest?

The ClassNotFound exception indicates that somewhere your admob library isn't connecting to your XML. There are a few things that can cause this, but the most likely are either that it's not declared in the manifest:

 <application android:icon="@drawable/icon" android:label="@string/app_name"
           android:debuggable="true">
  <activity android:label="@string/app_name" android:name="yourActivity">
       .....
  </activity>
  <activity android:name="com.google.ads.AdActivity"   <----- this line
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
 </application>

or that it's not declared at the top of your XML file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"   <----- this line
          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>

Try those out, and if they don't do the trick check out Google's documentation here and here

Upvotes: 2

Related Questions