Question

I have an error in my xml file, also it comes when i start project. I added libs, also added google-play-services_lib as android developer says but nothing changes

The error is:

The following classes could not be instantiated:
- com.google.ads.AdView (Open Class, Show Error Log)
See the Error Log (Window > Show View) for more details.

  java.lang.NoSuchMethodError: com.google.ads.AdSize.createAdSize(Lcom/google/ads/AdSize;Landroid/content/Context;)Lcom/google/ads/AdSize;
        at com.google.ads.AdView.a(SourceFile:161)
        at com.google.ads.AdView.a(SourceFile:352)
        at com.google.ads.AdView.<init>(SourceFile:125)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(    at sun.reflect.NativeConstructorAccessorImpl.newInstance(    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(    at java.lang.reflect.Constructor.newInstance(    at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.instantiateClass(ProjectCallback.java:437)
        at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.loadView(ProjectCallback.java:189)
        at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207)
        at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:135)
        at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:755)

My code:

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:text="hallo" />
    </LinearLayout>



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

    public class MainActivity extends Activity {
        private AdView adView;
          @Override
          public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            // Создание экземпляра adView.
            adView = new AdView(this);
            adView.setAdUnitId("2062620609988745/6577956317"); // MY_AD_UNIT_ID 2062620609988745/6577956318
            adView.setAdSize(AdSize.BANNER);
            AdRequest.Builder builder = new AdRequest.Builder();
            builder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);

            // Поиск разметки LinearLayout (предполагается, что ей был присвоен
            // атрибут android:id="@+id/mainLayout").
            LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout1);

            // Добавление в разметку экземпляра adView.
            layout.addView(adView);

            // Инициирование общего запроса.
            AdRequest adRequest = new AdRequest.Builder().build();

            // Загрузка adView с объявлением.
            adView.loadAd(adRequest);
          }

          @Override
          public void onPause() {
            adView.pause();
            super.onPause();
          }

          @Override
          public void onResume() {
            super.onResume();
            adView.resume();
          }

          @Override
          public void onDestroy() {
            adView.destroy();
            super.onDestroy();
          }
        }
Was it helpful?

Solution

You have a conflict in your Admob libraries.

You reference Admob 6.4.1 classes in your XML, but you are included Google play Services which uses different classes for Admob.

See https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals for using banner ads with Google play Services. Note the classes that are used.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top