1
Could 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 coming into conflict 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 it was this code , and it worked, so 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.com.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)
Did you receive an error message? If yes, add your question
– jlHertel
If you leave only the 1 and 2 line works?
– PauloHDSousa
R.id.adView
exists in the layout activity_main?– ramaral
Yes, there’s a strange thing about making that mistake
– Paiva
The error indicates that
mAdView
is null, I can find no other reason for this to happen than that it does not exist in the layout activity_main– ramaral
Tried to rename "adView" to any other name in XML? Because it may be conflicting with other Ids.
– itscorey
And you initiated ADMOB?
– itscorey