gpt4 book ai didi

java - Android - Google Play 服务抛出 IllegalStateException 提示尺寸和广告单元 ID

转载 作者:行者123 更新时间:2023-11-29 08:48:54 25 4
gpt4 key购买 nike

自从我将 Admob 库替换为 Google Play 服务库后,我的 Android 应用有时会触发以下异常

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.XXX.XXX/com.XXX.XXX.ui.activity.FeedActivity}: java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2328)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386)
at android.app.ActivityThread.access$900(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5476)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called.
at com.google.android.gms.internal.ak.an(Unknown Source)
at com.google.android.gms.internal.ak.a(Unknown Source)
at com.google.android.gms.ads.AdView.loadAd(Unknown Source)
at com.XXX.newslibrary.ui.activity.FeedActivity.initActivityLayout(FeedActivity.java:255)
at com.XXX.newslibrary.ui.activity.FeedActivity.onCreate(FeedActivity.java:72)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
... 11 more

基本上,异常是提示我没有在 loadAd 之前设置广告尺寸和单元 ID。这是我的代码:

    mAdView = new AdView(this);
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId(SourceActivity.APPLICATION_ADMOB_BANNER_ID);

LinearLayout layout = (LinearLayout) findViewById(R.id.activity_view);
layout.addView(mAdView);

AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
mAdView.setVisibility(View.GONE);

mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
Log.d(TAG, "onAdLoaded");
mAdView.setVisibility(View.VISIBLE);
}
});

如您所见,一切都是正确的。正如我之前所说,异常并不总是出现,实际上它只出现了几次。

有没有人知道是什么原因造成的?

最佳答案

修改代码

                  <com.google.android.gms.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/gridView1"
ads:adUnitId="MY ID NUMBER"
ads:adSize="BANNER"/>

                 <com.google.android.gms.ads.AdView android:id="@+id/adView"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adUnitId="ca-app-pub-8510605212168324/8634800090"
ads:adSize="BANNER"
android:layout_alignParentBottom="true"
>

</com.google.android.gms.ads.AdView>

关于java - Android - Google Play 服务抛出 IllegalStateException 提示尺寸和广告单元 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23700356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com