0

I get this error when I insert an interstitial ad. Help me

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mBinding = DataBindingUtil.setContentView(this, R.layout.module_home_activity_home);
    mViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
    getLifecycle().addObserver(mViewModel);

    initView();
    subscribeUi();

    MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");

    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    mInterstitialAd.loadAd(new AdRequest.Builder().build());

    Button tvOk = (Button) findViewById(R.id.tvOk);

    tvOk.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
            }
        }
    });

    mPermissionReqHandler = new PermissionReqHandler(self());
}

Logcat Message:

FATAL EXCEPTION: main
Process: com.xxx.xxx, PID: 29573
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx.xxx/com.xxx.xxx.app.tally.module.home.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    at com.coderpage.mine.app.tally.module.home.HomeActivity.onCreate(HomeActivity.java:86)
    at android.app.Activity.performCreate(Activity.java:6975)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
    at android.os.Handler.dispatchMessage(Handler.java:105) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6541) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
NewMB
  • 27
  • 5

1 Answers1

1

First, you need to set the content view in your onCreate function like the following.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set the content view like this
    setContentView(this, R.layout.module_home_activity_home);

    mBinding = DataBindingUtil.setContentView(this, R.layout.module_home_activity_home);
    mViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
    getLifecycle().addObserver(mViewModel);

    initView();
    subscribeUi();

    MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");

    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    mInterstitialAd.loadAd(new AdRequest.Builder().build());

    Button tvOk = (Button) findViewById(R.id.tvOk);

    tvOk.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
            }
        }
    });

    mPermissionReqHandler = new PermissionReqHandler(self());
}

And secondly, please check if the id of the button is set to tvOk in your module_home_activity_home.xml file.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98