Ad

Why I Can't Show Admob Rewarded Video Ads With Andengine In Android Studio?

I have a game with andengine in android studio . I want to show admob rewarded video ads in my app. But I can't show it. is there someone who can help me ?

I run the application on my own device (Xiaomi mi 6). The application started correctly on my own device . But The application closes when I click on the ad show button("anaMenuEngland.oSprite").

MainActivity.java

public class MainActivity extends BaseGameActivity implements RewardedVideoAdListener {

    private static final int CAMERA_WIDTH = 800;
    private static final int CAMERA_HEIGHT = 480;
    private Camera camera;
    private Engine engine;
    private Nesne anaMenuArka,anaMenuEngland,bayrakhover;
    Scene sahneOyun;

    @Override
    public Engine onLoadEngine() {
        camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
        final EngineOptions engineOptions = new EngineOptions(true, EngineOptions.ScreenOrientation.LANDSCAPE, new FillResolutionPolicy(), camera);
        engineOptions.getTouchOptions().setRunOnUpdateThread(true);
        engine = new Engine(engineOptions);

        return engine;
    }

    @Override
    public void onLoadResources() {
        anaMenuArka = new Nesne(1024, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA, this, "gfx/arkaplan.png", 0, 0,null);
        anaMenuEngland=new Nesne(64,32,TextureOptions.BILINEAR_PREMULTIPLYALPHA,this,"gfx/england.png",0,0,null);
        bayrakhover=new Nesne(64,32,TextureOptions.BILINEAR_PREMULTIPLYALPHA,this,"gfx/bayrakhover.png",0,0,null);

        Texture[]textures = {anaMenuArka.oTexture,anaMenuEngland.oTexture,bayrakhover.oTexture};
        this.engine.getTextureManager().loadTextures(textures);
    }

    @Override
    public Scene onLoadScene() {

        anaMenuArka.oSprite = new Sprite(0, 0, anaMenuArka.oTextureRegion);
        bayrakhover.oSprite = new Sprite(0, 200, bayrakhover.oTextureRegion);
        anaMenuEngland.oSprite = new Sprite(0, 248, anaMenuEngland.oTextureRegion)
        {
            @Override
            public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
                                         float pTouchAreaLocalX, float pTouchAreaLocalY)
            {
                if(pSceneTouchEvent.isActionDown())
                {
                }
                if(pSceneTouchEvent.isActionUp())
                {
                 //when this section is clicked to close the application (error)
                    if (mRewardedVideoAd.isLoaded()) {
                        mRewardedVideoAd.show();
                    }

                }
                return true;
            }
        };


        this.sahneOyun = new Scene();
        this.sahneOyun.attachChild(anaMenuArka.oSprite);
        this.sahneOyun.attachChild(anaMenuEngland.oSprite);
        this.sahneOyun.attachChild(bayrakhover.oSprite);

        this.sahneOyun.registerTouchArea(anaMenuEngland.oSprite);

        return sahneOyun;
    }
    private RewardedVideoAd mRewardedVideoAd;
    @Override
    public void onLoadComplete() {
        MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");

        mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
        mRewardedVideoAd.setRewardedVideoAdListener((RewardedVideoAdListener) this);
        loadRewardedVideoAd();

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

    }

    @Override
    public void onRewardedVideoAdLoaded() {

    }

    @Override
    public void onRewardedVideoAdOpened() {

    }

    @Override
    public void onRewardedVideoStarted() {

    }

    @Override
    public void onRewardedVideoAdClosed() {
        loadRewardedVideoAd();
    }

    @Override
    public void onRewarded(RewardItem rewardItem) {

    }

    @Override
    public void onRewardedVideoAdLeftApplication() {

    }

    @Override
    public void onRewardedVideoAdFailedToLoad(int i) {

    }

    @Override
    public void onRewardedVideoCompleted() {

    }
}

Nesne.java

public class Nesne{

    public int oTextureWidth, oTextureHeight;
    public Texture oTexture;
    public TextureRegion oTextureRegion;
    public Sprite oSprite;
    public TimerHandler timer;

    public Nesne(int oTextureWidth, int oTextureHeight, TextureOptions oTextureOptions, Context oContext, String oAssetPath, int oTexturePositionX, int oTexturePositionY, ITextureSource oTextureSource)
    {
        this.oTextureWidth = oTextureWidth;
        this.oTextureHeight = oTextureHeight;
        oTexture = new Texture(oTextureWidth, oTextureHeight, oTextureOptions);
        if (oAssetPath==null){
            oTextureRegion=TextureRegionFactory.createFromSource(oTexture,oTextureSource,oTexturePositionX,oTexturePositionY);
        }else{
            oTextureRegion = TextureRegionFactory.createFromAsset(oTexture, oContext, oAssetPath, oTexturePositionX, oTexturePositionY);
        }

    }

    public Nesne() {

    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.deneme">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-3940256099942544~3347511713"/>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Ad

Answer

You have to call ads related methods from Main thread. Simply wrap your code with runOnUiThread method:

runOnUiThread(new Runnable() {
   @Override
   public void run() {
       if (mRewardedVideoAd.isLoaded()) {
           mRewardedVideoAd.show();
       }
   }
});
Ad
source: stackoverflow.com
Ad