Kar*_*nak 5 java android admob
我想将一个源代码从旧更新GoogleAds SDK到新Google Play Service Library,但是有一个问题.每次我收到此错误:
获得广告响应时出现问题.
ErrorCode:0
无法加载广告:0
这是来自playactivty的代码:
public String BANNER_AD_UNIT_ID;
AdRequest adRequest;
private void LoadAds()
{
LinearLayout layout = (LinearLayout) findViewById(R.id.adView);
this.BANNER_AD_UNIT_ID = getResources().getString(R.string.admob_id);
// Create the adView
AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(BANNER_AD_UNIT_ID);
// Add the adView to it
layout.addView(adView);
// Initiate a generic request to load it with an ad
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
// AdRequest.setTesting(true);
adView.loadAd(adRequest);
}
Run Code Online (Sandbox Code Playgroud)
并从layout.xml
</RelativeLayout>
<com.google.android.gms.ads.AdView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="@string/admob_id">
</com.google.android.gms.ads.AdView>
<LinearLayout
android:orientation="horizontal"
android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
那里我做错了什么?找不到问题,相信我,我读了所有关于那个错误的SOF帖子:)
非常感谢你!
编辑:
原始代码:
PlayActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
LoadConfigParams();
LoadSharedPreferences();
LoadResources();
LoadListeners();
LoadStage(mCurStage);
LoadAds();
}
private void LoadAds()
{
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayoutAdmob);
// Create the adView
AdView adView = new AdView(this, AdSize.SMART_BANNER, getResources().getString(R.string.admob_id));
// Add the adView to it
layout.addView(adView);
// Initiate a generic request to load it with an ad
AdRequest request = new AdRequest();
request.setTesting(true);
adView.loadAd(request);
}
Run Code Online (Sandbox Code Playgroud)
activity_play.xml (只有最后一个LinearLayout代码,上面是RelativeLayout).
<LinearLayout
android:id="@+id/linearLayoutAdmob" android:layout_width="fill_parent"
android:layout_height="wrap_content"></LinearLayout>
Run Code Online (Sandbox Code Playgroud)
有两种方法可以Admob通过java或来创建添加xml,最简单的方法(尽管两者都很简单)是xml。此外,如果您使用,SMART_BANNER您将获得不同尺寸的添加,并且如果尺寸不适合/匹配剩余屏幕您为添加RelativeLayout留下的空间将不会显示。LinearLayout所以我假设你已经解决了这个问题
<LinearLayout
android:id="@+id/linearLayoutAdmob"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<com.google.android.gms.ads.AdView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
android:id="@+id/myaddview"
ads:adUnitId="@string/admob_id">
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
然后在你的loadAds()
AdView adView = findViewById(R.id.myaddview); //add the cast
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
adView.loadAd(adRequest);
Run Code Online (Sandbox Code Playgroud)
这就是您需要检查的内容以了解要导入的内容并仔细检查您的权限