我有关在Android应用中添加Google广告的问题

时间:2018-12-02 05:23:12

标签: android mobile

  

java.lang.NullPointerException:尝试调用虚拟方法'void   com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest)'   在空对象r上

public class Activity1 extends Activity {
    Button btnrateapp, btnnext,btnshareapp;
    int share,rate;
     private AdView adView;
    private InterstitialAd mInterstitialAd;
    int i = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity1);
       // MobileAds.initialize(this, "ca-app-pub-3297215203354227~5525786209");
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-9817937406815779/5109921994");
        mInterstitialAd.loadAd(new AdRequest.Builder().build());
        adView =findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        //mAdView.loadAd(new AdRequest.Builder().build());
         adView.loadAd(adRequest);

1 个答案:

答案 0 :(得分:1)

您必须在布局activity1.xml中添加以下代码:

<com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="ad_unit_id_for_banner_here"/>