0

I added Admob to my app, but it causes it to close when I open the app.

In the logcat, I found this: Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.ads.AdView"

I searched for that and it talked about using an older version of the Admob SDK

I'm using the Google Play SDK google-play-services_lib.

Here is my Java file.

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.LinearLayout;

public class MainActivity extends ActionBarActivity {


    WebView myWebView;
    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        getActionBar().setDisplayHomeAsUpEnabled(false);

        String url = "file:///android_asset/index.html";

            myWebView = (WebView) findViewById(R.id.webView1);
            //next line explained below
            myWebView.setWebViewClient(new MyWebViewClient());
            myWebView.getSettings().setJavaScriptEnabled(true);
            myWebView.loadUrl(url);


            AdView adView = new AdView(this);

            // Lookup your LinearLayout assuming it's been given
            // the attribute android:id="@+id/mainLayout"
            LinearLayout layout = (LinearLayout)findViewById(R.id.adView);

            // Add the adView to it
            layout.addView(adView);

            AdRequest request = AdRequest();
            adView.loadAd(request);
    }



    private AdRequest AdRequest() {
        // TODO Auto-generated method stub
        return null;
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }
    @Override
    public void onBackPressed() {
        if(myWebView.canGoBack() == true){
            myWebView.goBack();
        }else{
            super.onBackPressed();
        }
    }

}

Logcat

05-25 17:29:26.038: E/AndroidRuntime(23325): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.ads.AdView" on path: DexPathList[[zip file "/data/app/com.jkb.pocketwelderhelperfree-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.jkb.pocketwelderhelperfree-1, /vendor/lib, /system/lib]]
peedeefish
  • 27
  • 6

2 Answers2

2

Somewhere in your app you are referencing com.google.ads.AdView which is the old (6.4.1 and before) Admob classes. Find out where that is and remove it.

Perhaps in your XML? Or in a library that you are including?

William
  • 20,150
  • 8
  • 49
  • 91
0

I had the same problem. Perhaps I'm late but it could help others.

Check this please: In the step 3 of Setting Up Google Play Services says:

In your app project, reference Google Play services library project. See Referencing a Library Project for Eclipse for more information on how to do this.

Note: You should be referencing a copy of the library that you copied to your development workspace—you should not reference the library directly from the Android SDK directory.

Solution: Don't do the copy manually. Instead just check the box in Eclipse that says "Copy projects into workspace"

I did it so and everything worked again.

Thanks to this StackOverFlow question: How to fix Google Play Services 2 Library install to Eclipse

Community
  • 1
  • 1
Ignacio Rubio
  • 1,354
  • 14
  • 25