1

native ad mobile opened. This error is causing this error for me. I tried to fix this error several times but I could not fix it. Native ad is causing this error.

Error

*E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 19618
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.AdView.setAdSize(com.google.android.gms.ads.AdSize)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3318)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3429)
        at android.app.ActivityThread.-wrap12(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009)
        at android.os.Handler.dispatchMessage(Handler.java:109)
        at android.os.Looper.loop(Looper.java:166)
        at android.app.ActivityThread.main(ActivityThread.java:7555)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:963)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.AdView.setAdSize(com.google.android.gms.ads.AdSize)' on a null object reference
        at com.example.myapplication.MainActivity.loadBanner(MainActivity.java:59)
        at com.example.myapplication.MainActivity.onCreate(MainActivity.java:38)
        at android.app.Activity.performCreate(Activity.java:7343)
        at android.app.Activity.performCreate(Activity.java:7333)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1219)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3271)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3429) 
        at android.app.ActivityThread.-wrap12(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009) 
        at android.os.Handler.dispatchMessage(Handler.java:109) 
        at android.os.Looper.loop(Looper.java:166) 
        at android.app.ActivityThread.main(ActivityThread.java:7555) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:963) 
I/Process: Sending signal. PID: 19618 SIG: 9*
SuperStormer
  • 4,997
  • 5
  • 25
  • 35
atozcodes
  • 1
  • 2
  • 13
  • Have you seen all the possible solutions here? https://stackoverflow.com/questions/39401662/how-to-solve-nullpointerexception-void-com-google-android-gms-ads-adview-loadad – zizutg Apr 28 '21 at 01:36

2 Answers2

0

I guess you are using the Admob Adaptive Banner. If so, then you are probably missing an instance of AdView. Make sure that you have the following code in onCreateView and call loadBanner() function after the instance of AdView is created:

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    adContainerView = findViewById(R.id.ad_view_container);

    adView = new AdView(this);
    adView.setAdUnitId(getString(R.string.adaptive_banner_ad_unit_id));
    adContainerView.addView(adView);
    loadBanner();
  }

From The Official Document:

Wilson Tran
  • 4,050
  • 3
  • 22
  • 31
0

you want to use AdSize from com.google.android.gms.ads and it returns null. you can declare the size of BANNER, or I recommend that you use Firebase - Ads. I hope I have helped.

Muhammad Saqlain
  • 2,112
  • 4
  • 33
  • 48