consentInformation.canRequestAds - 找不到符号变量 canRequestAds

Jul*_*lia 2 java android admob gdprconsentform

我正在尝试将 Google User Messaging Platform SDK 添加到我的应用程序中,以便它可以向 EEA 和英国用户投放广告。

我正在遵循本指南中的每一步。 https://developers.google.com/admob/android/privacy

但是,我在这行代码上收到错误“找不到符号变量 canRequestAds”consentInformation.canRequestAds

 import com.google.android.gms.ads.MobileAds;
 import com.google.android.material.tabs.TabLayout;
 import com.google.android.ump.ConsentForm;
 import com.google.android.ump.ConsentInformation;
 import com.google.android.ump.ConsentRequestParameters;
 import com.google.android.ump.UserMessagingPlatform;
 import java.util.concurrent.atomic.AtomicBoolean;

 public class MainActivity extends AppCompatActivity {
   private ConsentInformation consentInformation;
   // Use an atomic boolean to initialize the Google Mobile Ads SDK and load ads once.
   private final AtomicBoolean isMobileAdsInitializeCalled = new AtomicBoolean(false);

   @Override
   protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Set tag for under age of consent. false means users are not under age
// of consent.
ConsentRequestParameters params = new ConsentRequestParameters
    .Builder()
    .setTagForUnderAgeOfConsent(false)
    .build();

consentInformation = UserMessagingPlatform.getConsentInformation(this);
consentInformation.requestConsentInfoUpdate(
    this,
    params,
    (OnConsentInfoUpdateSuccessListener) () -> {
      UserMessagingPlatform.loadAndShowConsentFormIfRequired(
        this,
        (OnConsentFormDismissedListener) loadAndShowError -> {
          if (loadAndShowError != null) {
            // Consent gathering failed.
            Log.w(TAG, String.format("%s: %s",
                loadAndShowError.getErrorCode(),
                loadAndShowError.getMessage()));
          }

          // Consent has been gathered.
          if (consentInformation.canRequestAds) {
            initializeMobileAdsSdk();
          }
        }
      )
    },
    (OnConsentInfoUpdateFailureListener) requestConsentError -> {
      // Consent gathering failed.
      Log.w(TAG, String.format("%s: %s",
          requestConsentError.getErrorCode(),
          requestConsentError.getMessage()));
    });

// Check if you can initialize the Google Mobile Ads SDK in parallel
// while checking for new consent information. Consent obtained in
// the previous session can be used to request ads.
if (consentInformation.canRequestAds) {
  initializeMobileAdsSdk();
}
   }

   private void initializeMobileAdsSdk() {
if (isMobileAdsInitializeCalled.getAndSet(true)) {
  return;
}

// Initialize the Google Mobile Ads SDK.
MobileAds.initialize(this);

// TODO: Request an ad.
// InterstitialAd.load(...);
   }
 }
Run Code Online (Sandbox Code Playgroud)

小智 7

我遇到了同样的问题并反编译了ConsentInformation接口。Google 提供的代码存在错误 -canRequestAds不是字段而是方法,因此您应该canRequestAds()使用canRequestAds