'Fragment.java Rewarded Ad Implement Problem
I have made an Apps, but when I put the code of RewardedAd in Fragment.java, I can't find the keyword "this" correctly, which is giving Code Error. Now, what can I do to solve this error?*
public HomeFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
FragmentHomeBinding binding;
FirebaseFirestore database;
private RewardedAd mRewardedAd;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = FragmentHomeBinding.inflate(inflater, container, false);
AdRequest adRequest = new AdRequest.Builder().build();
RewardedAd.load(this, "ca-app-pub-3940256099942544/5224354917",
adRequest, new RewardedAdLoadCallback() {
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error.
mRewardedAd = null;
}
@Override
public void onAdLoaded(@NonNull RewardedAd rewardedAd) {
mRewardedAd = rewardedAd;
}
});```
Solution 1:[1]
The problem is in the context. One of the parameters of the load()
method is the context you pass this to. But the main problem is that when working with fragments, we must use the Activity context. Here is the solution to your problem - write requireActivity()
instead of this
and it will work.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Riggel |