廣告載入錯誤

廣告載入失敗時,一律會呼叫 回呼 ,藉此提供LoadAdError 物件。

對 an AdView來說,系統會呼叫下列程式碼:

下列程式碼片段說明廣告無法載入時可用的資訊:

Java

@Override
public void onAdFailedToLoad(LoadAdError error) {
  // Gets the domain from which the error came.
  String errorDomain = error.getDomain();
  // Gets the error code. See
  // https://developers.google.com/android/reference/com/google/android/gms/ads/AdRequest#constant-summary
  // for a list of possible codes.
  int errorCode = error.getCode();
  // Gets an error message.
  // For example "Account not approved yet". See
  // https://support.google.com/admob/answer/9905175 for explanations of
  // common errors.
  String errorMessage = error.getMessage();
  // Gets additional response information about the request. See
  // https://developers.google.com/admob/android/response-info for more
  // information.
  ResponseInfo responseInfo = error.getResponseInfo();
  // Gets the cause of the error, if available.
  AdError cause = error.getCause();
  // All of this information is available via the error's toString() method.
  Log.d("Ads", error.toString());
}

Kotlin

override fun onAdFailedToLoad(error: LoadAdError) {
    // Gets the domain from which the error came.
    val errorDomain = error.domain
    // Gets the error code. See
    // https://developers.google.com/android/reference/com/google/android/gms/ads/AdRequest#constant-summary
    // for a list of possible codes.
    val errorCode = error.code
    // Gets an error message.
    // For example "Account not approved yet". See
    // https://support.google.com/admob/answer/9905175 for explanations of
    // common errors.
    val errorMessage = error.message
    // Gets additional response information about the request. See
    // https://developers.google.com/admob/android/response-info for more
    // information.
    val responseInfo = error.responseInfo
    // Gets the cause of the error, if available.
    val cause = error.cause
    // All of this information is available via the error's toString() method.
    Log.d("Ads", error.toString())
}

這項資訊可用來更準確地判斷出廣告載入失敗的原因。 具體來說,如果是網域 MobileAds.ERROR_DOMAIN 中的錯誤,訊息 可以查詢這篇說明中心文章,取得更詳細的說明,以及該如何解決問題的建議做法。