Adview错误! - 不兼容的类型:视图无法转换为Adview

时间:2018-05-09 13:16:33

标签: android admob

我正在尝试在我的Android应用程序中加入Google Admob。我已根据https://developers.google.com/admob/android/quick-start?hl=en-US#import_the_mobile_ads_sdkhttps://developers.google.com/admob/android/banner?hl=en-US中的指南添加了代码。但是,我收到错误"不兼容的类型:视图无法转换为Adview"。

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



public class RecognizeActiv extends AppCompatActivity {

    public AdView mAdView;


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



        MobileAds.initialize(this,
                "ca-app-pub-1538792633238746~7576594574");

        mAdView = findViewById(R.id.adView); //getting error here.....
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);



    }

xml代码

 <com.google.android.gms.ads.AdView
                xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
                xmlns:android="http://schemas.android.com/apk/res/android"

                android:id="@+id/adView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_alignParentBottom="true"
                ads:adSize="SMART_BANNER"
                ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
            </com.google.android.gms.ads.AdView>

1 个答案:

答案 0 :(得分:3)

通过使用AdView本身进行Typecasting初始化AdView对象。

mAdView = (AdView)findViewById(R.id.adView);