android - AdRequest.Builder 无法解析为类型

标签 android admob

我正在将 AdMob 整合到我的应用中。我已经按照开发人员页面中的步骤进行操作。然而 AdRequest.Builder() 被标上了红色下划线,它说:

AdRequest cannot be resolved to a type

AdRequest.Builder cannot be resolved to a type.

可能是什么问题?

import com.google.ads.AdRequest;
import com.google.ads.AdView;


public class FireRoomActivity extends Activity {

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

        // Look up the AdView as a resource and load a request.
        AdView adView = (AdView)this.findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();   
        adView.loadAd(adRequest);
    }

我在 xml 中显示了 admob:

<com.google.android.gms.ads.AdView android:id="@+id/adView"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     ads:adUnitId="bla bla"
                     ads:adSize="BANNER"/>

最佳答案

您的代码是 Admob SDK (Google Play) 的混合代码和 Android (6.4.1 and earlier SDKs)

使用

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

AdRequest adRequest = new AdRequest.Builder().build();

<com.google.android.gms.ads.AdView android:id="@+id/adView"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     ads:adUnitId="bla bla"
                     ads:adSize="BANNER"/>

如果您使用 Admob SDK (Google Play)

或者使用

import com.google.ads.AdRequest;
import com.google.ads.AdView;

AdRequest adRequest = new AdRequest();

<com.google.ads.AdView android:id="@+id/adView"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     ads:adUnitId="bla bla"
                     ads:adSize="BANNER"/>

如果你使用早期的SDK

对于 Admob SDK (Google Play) 不要忘记更改 namespace

xmlns:ads="http://schemas.android.com/apk/res-auto"

关于android - AdRequest.Builder 无法解析为类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20607746/

相关文章:

Android布局方向问题

android WebView goBack 和 goForward 不适用于 WebViewClient

java - 在android项目中将所有名称变量和函数从大写更改为小写

android - 我正在使用 firebase_admob 在我的 flutter 应用程序中实现广告,但广告在 release build apk 中不起作用

android - 适用于 Android 的 Google Admob 问题 : Google Play Services were not found

android - Admob 不断返回 AdRequest.ERROR_CODE_NO_FILL?

android - 集成 admob 时 react native 中的不变违规 : TurboModuleRegistry. getEnforcing(...) : 'RNGoogleMobileAdsModule' ,

java - 单击按钮时将项目添加到 RecyclerView

android - Cordova :执行任务 ':processDebugManifest'失败。添加共享插件后

android - 为什么 adMob 在 Android 中抛出这些空指针异常?