我已将智能横幅广告替换为自适应横幅广告。但是现在,它在少数设备上崩溃了。
这是xml部分:
<FrameLayout xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
</FrameLayout>
Java部分:
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) { }
});
mAdView = new AdView(this);
mAdView.setAdUnitId(getString(R.string.ad_unit_id));
adContainer.addView(mAdView);
loadBanner();
loadBanner函数:
private AdSize getAdSize() {
// Step 2 - Determine the screen width (less decorations) to use for the ad width.
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
float widthPixels = outMetrics.widthPixels;
float density = outMetrics.density;
int adWidth = (int) (widthPixels / density);
// Step 3 - Get adaptive ad size and return for setting on the ad view.
return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth);
}
private void loadBanner() {
// Create an ad request. Check your logcat output for the hashed device ID
// to get test ads on a physical device, e.g.,
// "Use AdRequest.Builder.addTestDevice("ABCDE0123") to get test ads on this
// device."
AdRequest adRequest =
new AdRequest.Builder().addTestDevice("ASKDJALDSKJALD")
.build();
AdSize adSize = getAdSize();
// Step 4 - Set the adaptive ad size on the ad view.
mAdView.setAdSize(adSize);
// Step 5 - Start loading the ad in the background.
mAdView.loadAd(adRequest); //****Crashing on this line****
}
它在mAdView.loadAd(adRequest)
上崩溃了。
我查看了崩溃日志,它们是:
java.lang.RuntimeException:
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2976)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3113)
at android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:113)
at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:71)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1858)
at android.os.Handler.dispatchMessage (Handler.java:106)
at android.os.Looper.loop (Looper.java:201)
at android.app.ActivityThread.main (ActivityThread.java:6820)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:922)
Caused by: java.lang.IllegalStateException:
at com.google.android.gms.internal.ads.zzyq.zza (zzyq.java:82)
at com.google.android.gms.ads.BaseAdView.loadAd (BaseAdView.java:15)
at com.jazibkhan.equalizer.ui.activities.MainActivity.loadBanner (MainActivity.java:309)
at com.jazibkhan.equalizer.ui.activities.MainActivity.onCreate (MainActivity.java:341)
at android.app.Activity.performCreate (Activity.java:7224)
at android.app.Activity.performCreate (Activity.java:7213)
at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2956)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3113)
at android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks
(TransactionExecutor.java:113)
at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:71)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1858)
at android.os.Handler.dispatchMessage (Handler.java:106)
at android.os.Looper.loop (Looper.java:201)
at android.app.ActivityThread.main (ActivityThread.java:6820)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:922)
我已经完全完成了自适应广告documentation中显示的操作,但是仍然在少数设备上崩溃。