Error when using AdRequest

1

Could you help me find the error in this OnCreate method specifically in the AdRequest class?

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


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);


    viewPager = (ViewPager) findViewById(R.id.view_pager);
    viewPager.setAdapter(new CustomPageAdapter(getBaseContext()));

    mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
}

Some of the components are apparently conflicting with adRequest because I tested the:

mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);

In another application to test if this code was, and it worked, then the problem can only be in OnCreate

Below is the logcat error

  

FATAL EXCEPTION: main                                                                               Process: br.com.paivasdev.fazendocafe, PID: 5792                                                                               java.lang.RuntimeException: Unable to start activity   ComponentInfo {br.com.paivasdev.fazendocafe/br.com.paivasdev.fazendocafe.MainActivity}:   java.lang.NullPointerException: Attempt to invoke virtual method 'void   com.google.android.gms.ads.AdView.loadAd (com.google.android.gms.ads.AdRequest) '   on a null object reference                                                                                   at   android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3319)                                                                                   at   android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3415)                                                                                   at android.app.ActivityThread.access $ 1100 (ActivityThread.java:229)                                                                                   at   android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1821)                                                                                   at android.os.Handler.dispatchMessage (Handler.java:102)                                                                                   at android.os.Looper.loop (Looper.java:148)                                                                                   at android.app.ActivityThread.main (ActivityThread.java:7325)                                                                                   at java.lang.reflect.Method.invoke (Native Method)                                                                                   at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:1230)                                                                                   at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1120)                                                                                Caused by: java.lang.NullPointerException: Attempt to invoke virtual   method 'void   com.google.android.gms.ads.AdView.loadAd (com.google.android.gms.ads.AdRequest) '   on a null object reference                                                                                   at   br.paivasdev.fazendocafe.MainActivity.onCreate (MainActivity.java:50)                                                                                   at android.app.Activity.performCreate (Activity.java:6904)                                                                                   at   android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1136)                                                                                   at   android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3266)                                                                                   at   android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3415)                                                                                   at android.app.ActivityThread.access $ 1100 (ActivityThread.java:229)                                                                                   at   android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1821)                                                                                   at android.os.Handler.dispatchMessage (Handler.java:102)                                                                                   at android.os.Looper.loop (Looper.java:148)                                                                                   at android.app.ActivityThread.main (ActivityThread.java:7325)                                                                                   at java.lang.reflect.Method.invoke (Native Method)

asked by anonymous 22.03.2017 / 17:04

1 answer

2

After Creating an AdMob account , see below for a summary and functional for creating news banners in your application and make a comparison with your code.

build.gradle :

Check out the latest Firebase release to import the libs on your project.

dependencies {
     compile 'com.google.firebase:firebase-ads:9.6.1'
}

main.xml

It's important not to forget to use xmlns:ads="http://schemas.android.com/apk/res-auto" in XML. See:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent" 
    tools:context=".MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>

</RelativeLayout>

MainActivity.java

package ...

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

public class MainActivity extends ActionBarActivity {

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

        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    }
}

AndroidManifest.xml

Finally give your application permission to access the internet. If you do not have permission, banners will not appear.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    
22.03.2017 / 18:11