Admob奖励视频广告无法加载片段。 我认为这两行存在问题
mAd = MobileAds.getRewardedVideoAdInstance(getActivity());
mAd.setRewardedVideoAdListener(this);
请检查以下代码。 代码在Activity中正常工作,但在Fragment中不起作用。
public class FragmentWatchEarn extends Fragment implements RewardedVideoAdListener {
int coins = 0;
View rootView;
Button bAdmob;
TextView tvCoins;
private RewardedVideoAd mAd;//Admob
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_watch_earn, container, false);
bAdmob = (Button) rootView.findViewById(R.id.btnAdmob);
admobAdsPart();
return rootView;
}
private void admobAdsPart() {
mAd = MobileAds.getRewardedVideoAdInstance(getActivity()); //Might me Problem in this line
mAd.setRewardedVideoAdListener(this); //Might me Problem in this line
loadAd();
bAdmob.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mAd.isLoaded()){
mAd.show();
}else {
Toast.makeText(getActivity(), "Ad not yet loaded", Toast.LENGTH_SHORT).show();
loadAd();
}
}
});
}
private void loadAd() {
if (!mAd.isLoaded()) {
mAd.loadAd("ca-app-pub-5059726881726792/4575431467", new AdRequest.Builder().build());
}
}
@Override
public void onRewardedVideoAdLoaded() {
}
@Override
public void onRewardedVideoAdOpened() {
}
@Override
public void onRewardedVideoStarted() {
}
@Override
public void onRewardedVideoAdClosed() {
}
@Override
public void onRewarded(RewardItem rewardItem) {
coins = coins + 20;
myRef.setValue("" + coins);
}
@Override
public void onRewardedVideoAdLeftApplication() {
}
@Override
public void onRewardedVideoAdFailedToLoad(int i) {
Toast.makeText(getActivity(), "Admob Ad Failed to load", Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:4)
此代码适用于我。
使用此代码,您无需在XXXXXFragment.java的顶部实现OnRewardListener
private RewardedVideoAd mRewardedVideoAd;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_main, container, false);
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(getContext());
mRewardedVideoAd.setRewardedVideoAdListener(rewardedVideoAdListener);
CircleImageView circleImageView = (CircleImageView)rootView.findViewById(R.id.btn_play);
circleImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
loadRewardedVideoAd();
}
});
return rootView;
}
RewardedVideoAdListener rewardedVideoAdListener = new RewardedVideoAdListener() {
@Override
public void onRewardedVideoAdLoaded() {
Toast.makeText(getActivity(), "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdOpened() {
Toast.makeText(getActivity(), "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoStarted() {
Toast.makeText(getActivity(), "onRewardedVideoStarted", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdClosed() {
Toast.makeText(getActivity(), "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show();
////// UpdateDataBase
}
@Override
public void onRewarded(RewardItem reward) {
Toast.makeText(getActivity(), getString(R.string.on_rewarded_video) + " " + reward.getAmount() + " " + reward.getType(), Toast.LENGTH_LONG).show();
// Reward the user.
}
@Override
public void onRewardedVideoAdLeftApplication() {
Toast.makeText(getActivity(), "onRewardedVideoAdLeftApplication", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdFailedToLoad(int i) {
Toast.makeText(getActivity(), "onRewardedVideoAdFailedToLoad", Toast.LENGTH_SHORT).show();
}
};
private void loadRewardedVideoAd() {
mRewardedVideoAd.loadAd("ca-app-pub-#################/##########",
new AdRequest.Builder().build());
}
我希望它可以帮到你
答案 1 :(得分:0)
尝试改变:
mAd = MobileAds.getRewardedVideoAdInstance(getContext());
我有相同的代码并且正在运作。