I want to implement rewarded video on my android application, the first time i click to show video, is works just fine, but after that the second or the third time i click to show it again, it doesn't work, and throws on the log :
W/MessageQueue: Handler (com.google.android.gms.ads.exoplayer2.upstream.u) {1e5487c} sending message to a Handler on a dead thread
java.lang.IllegalStateException: Handler (com.google.android.gms.ads.exoplayer2.upstream.u) {1e5487c} sending message to a Handler on a dead thread
at android.os.MessageQueue.enqueueMessage(MessageQueue.java:543)
at android.os.Handler.enqueueMessage(Handler.java:631)
at android.os.Handler.sendMessageAtTime(Handler.java:600)
at android.os.Handler.sendMessageDelayed(Handler.java:570)
at android.os.Handler.sendEmptyMessageDelayed(Handler.java:534)
at android.os.Handler.sendEmptyMessage(Handler.java:519)
at com.google.android.gms.ads.exoplayer2.upstream.u.run(:com.google.android.gms.DynamiteModulesA:212)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Could not show rewarded video ad from adapter.
java.lang.NullPointerException: Attempt to read from field 'com.google.android.gms.ads.internal.client.bq ip.a' on a null object reference
at com.google.ads.mediation.a.g(:com.google.android.gms.DynamiteModulesA:17227)
at com.google.android.gms.ads.internal.mediation.client.u.f(:com.google.android.gms.DynamiteModulesA:436)
at com.google.android.gms.ads.internal.reward.b.a(:com.google.android.gms.DynamiteModulesA:1314)
at com.google.android.gms.ads.internal.reward.client.e.onTransact(:com.google.android.gms.DynamiteModulesA:65)
at android.os.Binder.transact(Binder.java:387)
at com.google.android.gms.internal.zznu$zza$zza.show(Unknown Source)
at com.google.android.gms.internal.zzoc.show(Unknown Source)
at com.reperfection.monopoly.MainActivity$1.onClick(MainActivity.java:70)
at android.view.View.performClick(View.java:5702)
at android.view.View$PerformClick.run(View.java:22546)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7237)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Could not call showVideo.
android.os.RemoteException
at com.google.android.gms.ads.internal.mediation.client.u.f(:com.google.android.gms.DynamiteModulesA:439)
at com.google.android.gms.ads.internal.reward.b.a(:com.google.android.gms.DynamiteModulesA:1314)
at com.google.android.gms.ads.internal.reward.client.e.onTransact(:com.google.android.gms.DynamiteModulesA:65)
at android.os.Binder.transact(Binder.java:387)
at com.google.android.gms.internal.zznu$zza$zza.show(Unknown Source)
at com.google.android.gms.internal.zzoc.show(Unknown Source)
at com.reperfection.monopoly.MainActivity$1.onClick(MainActivity.java:70)
at android.view.View.performClick(View.java:5702)
at android.view.View$PerformClick.run(View.java:22546)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7237)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
This is my code :
public class MainActivity extends AppCompatActivity implements RewardedVideoAdListener {
TextView counterTV;
ImageView clickerBoard;
int counter;
int step = 1;
RelativeLayout parentRl;
LinearLayout ln1;
LinearLayout ln2;
private RewardedVideoAd mAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
setContentView(R.layout.activity_main);
counterTV = (TextView) findViewById(R.id.counter);
parentRl = (RelativeLayout) findViewById(R.id.parentRl);
clickerBoard = (ImageView) findViewById(R.id.clickerBoard);
ln1 = (LinearLayout) findViewById(R.id.ln1);
ln2 = (LinearLayout) findViewById(R.id.ln2);
// Init rewarded video
mAd = MobileAds.getRewardedVideoAdInstance(this);
mAd.setRewardedVideoAdListener(this);
loadRewardedVideoAd();
ln1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mAd.isLoaded()) {
mAd.show();
}
}
});
}
private void loadRewardedVideoAd() {
mAd.loadAd("ca-app-pub-3940256099942544/5224354917", new AdRequest.Builder().build());
}
@Override
public void onRewarded(RewardItem reward) {
Toast.makeText(this, "onRewarded! currency: " + reward.getType() + " amount: " +
reward.getAmount(), Toast.LENGTH_SHORT).show();
// Reward the user.
}
// The following listener methods are optional.
@Override
public void onRewardedVideoAdLeftApplication() {
Toast.makeText(this, "onRewardedVideoAdLeftApplication",
Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdClosed() {
Toast.makeText(this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show();
loadRewardedVideoAd();
}
@Override
public void onRewardedVideoAdFailedToLoad(int errorCode) {
Toast.makeText(this, "onRewardedVideoAdFailedToLoad", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdLoaded() {
Toast.makeText(this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdOpened() {
Toast.makeText(this, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoStarted() {
Toast.makeText(this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show();
}
@Override
public void onResume() {
mAd.resume(this);
super.onResume();
}
@Override
public void onPause() {
mAd.pause(this);
super.onPause();
}
@Override
public void onDestroy() {
mAd.destroy(this);
super.onDestroy();
}}
Can you help to figure out what is wrong ? Thanks
SOLUTION
I faced the same issue and looking to the official example I found that they are not calling
RewardedVideoAd.destroy(Context)
Although they tell to call it in their official tutorial. So after removing it, it worked.
I'm placing an admob video, which is executed when you enter the activity, I had the same problem when entering the activity for the second time. In my case I put an executable loop to start the video when it is already loaded.
if (mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.show();
}else{
loadRewardedVideoAd();
}
This is part of all my code
private boolean started = false;
private Handler handler = new Handler();
private void initializeAdMob() {
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
mRewardedVideoAd.setRewardedVideoAdListener(this);
loadRewardedVideoAd();
}
private void loadRewardedVideoAd() {
if (!mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.loadAd(getString(R.string.id_block_activity), new AdRequest.Builder().build());
}
}
private void showRewardedVideoAd() {
if (BaseInteractor.isNetworkAvailable(this)) {
showProgressBar(true);
start();
}
}
private Runnable runnable = new Runnable() {
@Override
public void run() {
if (mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.show();
}else{
loadRewardedVideoAd();
}
if (started) {
start();
}
}
};
public void stop() {
started = false;
handler.removeCallbacks(runnable);
}
public void start() {
started = true;
handler.postDelayed(runnable, 2000);
}
finally I stop the runnable in
@Override
public void onRewardedVideoAdOpened() {
showProgressBar(false);
stop();
}
I hope this helps you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With