NullPointerException on Google Banner Ads

245 views Asked by At

my app get crashing frequently due to NullPointerException and Native crash and thus I am losing my play store rank day by day. someone please help me on this.. is there any way if banner ad is not ready, can I show native ad (optional question)?

NullPointerException at: mAdView.loadAd(new AdRequest.Builder().build());

My app build version 26.0.1 and using Android Studio 3.0 Beta 2.

my code:

public class B1 extends Fragment {


    public B1() {
        // Required empty public constructor
    }

    private AdView mAdView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_b1, container, false);


//Banner
        mAdView = rootView.findViewById(R.id.adView);
        mAdView.loadAd(new AdRequest.Builder().build());
        mAdView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                mAdView.setVisibility(View.VISIBLE);
            }
            @Override
            public void onAdFailedToLoad(int error) {
                mAdView.setVisibility(View.GONE);
            }

        });

        return rootView;
    }

}
1

There are 1 answers

0
martin1m On

The banner is ready. Your findViewById is returning null. Make sure rootView actually contains the AdView R.id.adView