我已经添加了这段代码来查看应用程序无法接收广告时的错误。
// 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);
// Set AdListener
adView.setAdListener(new AdListener() {
@Override
public void onFailedToReceiveAd(Ad ad, ErrorCode error) {
System.err.println("Ad failed: " + ad.toString() + error.toString());
}
@Override
public void onReceiveAd(Ad ad) {
System.out.println("Ad received: " + ad.toString());
}
});
}
不幸的是,因为找不到 Ad
和 ErrorCode
可能是因为在最新的库中已经改变了一些东西。
我该如何解决这个问题?
最佳答案
根据 google admob 新的 api 服务
步骤:
- 下载最新的 Google Play 服务库
BannerCodeActivity.java
public class BannerCodeActivity extends Activity { private AdView mAdView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_banner_code_ad_listener); mAdView = new AdView(this); mAdView.setAdUnitId(getResources().getString(R.string.ad_unit_id)); mAdView.setAdSize(AdSize.BANNER); mAdView.setAdListener(new ToastAdListener(this)); RelativeLayout layout = (RelativeLayout) findViewById(R.id.mainLayout); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); layout.addView(mAdView, params); mAdView.loadAd(new AdRequest.Builder().build()); } @Override protected void onPause() { mAdView.pause(); super.onPause(); } @Override protected void onResume() { super.onResume(); mAdView.resume(); } @Override protected void onDestroy() { mAdView.destroy(); super.onDestroy(); } }
明确许可
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
您必须关注所有这些来自 here 的更多信息.
如果有任何问题,请告诉我。
关于android - AdMob 广告和 ErrorCode 无法解析为类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20483142/