AdMob Ad和ErrorCode无法解析为某种类型

时间:2013-12-09 23:39:44

标签: android admob google-play-services

如果App无法接收广告,我已添加此代码以查看错误。

    // 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());
        }
    });
  }

遗憾的是,由于无法找到AdErrorCode,因为最新的库已经改变了一些东西,因此无效。

我该如何解决这个问题?

5 个答案:

答案 0 :(得分:3)

根据google admob新的api服务

<强>步骤

  1. 下载最新的Google Play服务库
  2. 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();
        }
    }
    
  3. 清单权限

    <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"/>
    
  4. 您必须从here了解更多信息。

    如果有任何问题,请告诉我。

答案 1 :(得分:1)

我认为你的意思是你的编译失败了。 NB在StackOverflow上发布实际的错误消息总是一个好主意。

检查您的进口商品。如果您使用Admob的Google Play服务库,那么该套餐已更改为com.google.android.gms.ads

com.google.android.gms.ads.AdListener的界面是

public abstract class AdListener {
  public void onAdLoaded();
  public void onAdFailedToLoad(int errorCode);
  public void onAdOpened();
  public void onAdClosed();
  public void onAdLeftApplication();
}

自从Admob-6.4.1以来,这已经更改了 您似乎正在使用Admob-6.4.1的AdListener

答案 2 :(得分:1)

您需要了解如何迁移到新API的所有内容都在this Google Play Services Migration document

但它没有提到的一件事是,替换旧onAdFailedToLoad的新onFailedToReceiveAd函数的错误代码值(现在是int值)。 这些内容在the AdListener reference pagethe AdRequest reference page

中有所描述

答案 3 :(得分:0)

我建议您按照Google Developer Site的步骤进行操作。我按照相同的方式成功查看模拟器和真实设备上的添加。您还应在Manifest.xml

中添加以下代码行
    <activity android:name="com.google.ads.AdActivity"
              android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" >
</activity>


您也可以从this Link

下载示例代码

答案 4 :(得分:0)

我有同样的错误,这个错误是因为google play服务库没有完全添加到你的项目中,右键单击错误并选择修复项目或转到项目 - &gt;属性 - &gt;单击java Build Paths然后转到项目并单击Add button s