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

时间:2014-08-22 06:33:10

标签: android eclipse admob

我将我的老朋友更新为新的adob。我下载了SDK,导入了google play服务库,并在google documantation中完成了所有工作。但是当我添加xml代码时:

<com.google.android.gms.ads.AdView android:id="@+id/adView"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     ads:adUnitId="ca-app-pub-XXXXXXXXXXXXXXX/YYYYYYYY"
                     ads:adSize="BANNER"/>

我在错误日志中收到以下消息:

The following classes could not be instantiated: - com.google.android.gms.ads.AdView

有Manifest XML代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testapp"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version"/>

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.google.android.gms.ads.AdActivity"
         android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>       
</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

你能帮我吗?

2 个答案:

答案 0 :(得分:0)

您可以在java代码中创建AdView,但不能在xml中创建。 它对我有用:

private AdView adView;
private static final String AD_UNIT_ID = "XXXXXXX";

@Override
public void onCreate(){
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   // Create an ad.
   adView = new AdView(this);
   adView.setAdSize(AdSize.BANNER);
   adView.setAdUnitId(AD_UNIT_ID);

   // Add the AdView to the view hierarchy. The view will have no size
   // until the ad is loaded.
   LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
   layout.addView(adView);

   // Create an ad request. Check logcat output for the hashed device ID to
   // get test ads on a physical device.
   AdRequest adRequest = new AdRequest.Builder()
    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
    .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
    .build();

   // Start loading the ad in the background.
   adView.loadAd(adRequest);
}
@Override
public void onResume() {
  super.onResume();
  if (adView != null) {
    adView.resume();
  }
}

@Override
public void onPause() {
  if (adView != null) {
    adView.pause();
  }
  super.onPause();
}

/** Called before the activity is destroyed. */
@Override
public void onDestroy() {
  // Destroy the AdView.
  if (adView != null) {
    adView.destroy();
  }
  super.onDestroy();
}

答案 1 :(得分:0)

您是否编辑了proguard-project.txt?如果没有,那么打开proguard-project.txt文件并将这些行放在那里......

-keep public class com.google.android.gms.ads.** {
         public *;
       }

      -keep public class com.google.ads.** {
         public *;
       }

在project.properties中设置target = android-13或更高版本。