1

I am trying to integrate Google Admob into my project, but in running into an issue. I will go over my project first.

My project is structure on 2 main Java files and around 7 .xml files. The .xml files are shapes, the .xml files are neither linear or relative layout. In my main Java file I draw those .xml file shapes to the screen. So I do not depend on a .xml layout for my project.

EXAMPLE OF SHAPE .XML FILE

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemes.android.com/apk/res/android"
>

<solid android:color="#000" />
<corners
android:bottomLeftRadius="15dp"
android:bottomRightRadius="15dp"
android:topLeftRaidus="17dp"
android:topRightRadius="17dp" />
</shape>

So I am trying to add Google ads into my project, so I will need to add the Google ad .xml code. But I'm not for sure where. When I put my ad .xml code in one of the shapes .xml code I get and error that says "You can not put this here".

So I thought a little bit and realized I still have my .xml layout file that was created when I created my project. So I add my ad .xml code there and had no error. So next I added my Java code in my mainactivity.java, into the oncreate, and I had no error.

I have 2 Java files, one is my mainactivity.java and the other one is my MV.java. My MV.java is where I draw all my .xml shapes to the screen.

My Java code looks like this:

protect void onCreate(bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = new MV(this);

Adview adView = (AdView) this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.builder().build();
adView.loadAd(adRequest);

}

I do not get any error with my Java code. So I thought I could run my application and have my ad. But when I run my app I get this error.

Attempt to invoke virtual method 'void com.google.android.gms.ads.Adview.loadAd(com.google.android.gms.ads.Adrequest)' on a null object reference 

I also did add the Google ad code in my build.gradle and androidmanifest.xml.

how can I get ads to work?

Jay Rathod
  • 11,131
  • 6
  • 34
  • 58

1 Answers1

0

You are getting the NullPointerException because adView is null.

You need to declare the AdView in your XML. It should be declared in the layout that is loaded by your Fragment/Activity.

William
  • 20,150
  • 8
  • 49
  • 91