无法实例化以下类: - com.google.android.gms.ads

时间:2014-11-22 19:15:31

标签: java android admob google-play-services

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-32763440xxxxxxxxxxxx" />

当我在图形布局中添加此代码时,会发现此错误,并且不会在设备上安装该应用程序。我该如何解决这个问题?

The following classes could not be instantiated:
  - com.google.android.gms.ads.AdView (Open Class, Show Error Log)
  See the Error Log (Window > Show View) for more details.
   java.lang.ClassNotFoundException: com.google.android.gms.R$styleable
     at java.net.URLClassLoader$1.run(    at java.net.URLClassLoader$1.run(    at            java.security.AccessController.doPrivileged(    at java.net.URLClassLoader.findClass(    at    java.lang.**strong text**ClassLoader.loadClass(    at java.lang.ClassLoader.loadClass(    at com.google.android.gms.internal.bb.<init>(    at com.google.android.gms.internal.bh.<init>(    at com.google.android.gms.internal.bh.<init>(    at com.google.android.gms.internal.bh.<init>(    at com.google.android.gms.ads.AdView.<init>(    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:442)
    at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.loadView(ProjectCallback.java:194)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:132)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:806)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:782)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:385)

1 个答案:

答案 0 :(得分:2)

确保在布局中定义了正确的xml命名空间:

因此,xmlns:ads="http://schemas.android.com/apk/res-auto"

<com.google.android.gms.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-32763440xxxxxxxxxxxx" />

在清单的应用程序标签中,不要忘记添加:

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

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