Google横幅广告上的NullPointerException

时间:2017-08-25 19:18:33

标签: nullpointerexception admob banner-ads google-admob

由于NullPointerException和Native崩溃,我的应用程序经常崩溃,因此我每天都在失去我的游戏商店排名。有人请帮我这个..如果横幅广告没有准备好,有什么办法,我可以展示原生广告(可选问题)吗?

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

我的应用构建版本26.0.1并使用Android Studio 3.0 Beta 2.

我的代码:

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 个答案:

答案 0 :(得分:0)

横幅已准备就绪。您的findViewById返回null。确保rootView实际包含AdView R.id.adView