android – Admob Rewarded ads without button-ThrowExceptions

Exception or error:

So my problem is actually pretty simple. I have a Rewardable admob adview, but it is triggered right now in a onclick method. How can I first some how load the Rewardvideoad and then display it without an onclick method ?

my oncreate method:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admob);


        weekname = getIntent().getExtras().getString("week");
        score = getIntent().getExtras().getString("score");
        username = getIntent().getExtras().getString("username");

        button4 = findViewById(R.id.button4);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mRewardedVideoAd.isLoaded()) {
                    mRewardedVideoAd.show();
                }
            }
        });

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

        loadRewardedVideoAd();

    }
private void loadRewardedVideoAd() {
    mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",
            new AdRequest.Builder().build());
}

Please help,

Thanks;)

How to solve:

This is not allowed to show the rewarded ad without opt-in.

Before displaying a rewarded ad to users, you must present the user
with an explicit choice to view rewarded ad content in exchange for a
reward. Rewarded ads must always be an opt-in experience.

Leave a Reply

Your email address will not be published. Required fields are marked *