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)