Alex
Alex

Reputation: 11

Android calling a class for ads

I am trying to implement ads in my android game. I have created a class called Ads and I am calling it from from another class inside a switch case by using the following method.

 public class ControlCenter1 extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_control_center1);

            public ControlCenter1(Context context)
    {
        mScene = ParrotsConstent.E_SCENARIO.MENU;
        mContext = context;
        mScore = new Score();
        mSound = new Sound(context);
        mTimer = new Timer(ParrotsConstent.MAX_TIME);
        mAnimalPic = new int[(int) ParrotsConstent.GRID_NUM][(int) ParrotsConstent.GRID_NUM];
        mPicBak = new int[(int) ParrotsConstent.GRID_NUM][(int) ParrotsConstent.GRID_NUM];
        mEffect = new int[(int) ParrotsConstent.GRID_NUM][(int) ParrotsConstent.GRID_NUM];
        mDisappearToken = new int[(int) ParrotsConstent.GRID_NUM][(int) ParrotsConstent.GRID_NUM];
        mToken = new ActionTokenPool();
        init();
    }

    public static Handler mHandler = new Handler(){
            @Override
            public void handleMessage(Message msg)
            {
                switch(msg.what)
                {
    case GAME_OVER_START:
                    {
                        CtlTip2 ctl = (CtlTip2) drawTip2.control;
                        ctl.init(ParrotsConstent.E_TIP.GAMEOVER.ordinal()); 
                        mSound.play(ParrotsConstent.E_SOUND.TIMEOVER);
                        break;
                    }

        case GAME_OVER_END:
            {
              Ads ads = new Ads();
              ads.Adss();
              break;
            }
    }
    }

The class with google ads is as follows:

public class Ads extends AppCompatActivity {

    InterstitialAd mInterstitialAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ads);
    }

    public void Adss() 
{
        AdRequest adRequest = new AdRequest.Builder().build();


        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId(getString(R.string.admob_interstitial_id));

        mInterstitialAd.loadAd(adRequest);

        mInterstitialAd.setAdListener(new AdListener() {
            public void onAdLoaded() {
                displayInterstitial();
                onPause();
            }

            public void onAdClosed() {
                onResume();
                mScene = ParrotsConstent.E_SCENARIO.RESULT;
            }
        });
    }

    public void displayInterstitial() {
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        }
    }
}

Without ads the game runs smooth. I am not getting errors before I run the game but once I run the game and I get to game over the game stops with the following fatal errors.

FATAL EXCEPTION: main Process: PID: 2566

java.lang.NullPointerException: Attempt to invoke virtual method android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:86)
at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:127)                                                                                at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:121)                                                                               atandroid.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:549)
at android.content.Context.getString(Context.java:476)
at com.gamingtechnology.parrots.Ads.Adss(Ads.java:69)
at com.gamingtechnology.parrots.Core.ControlCenter1$2.handleMessage(ControlCenter1.java:1188)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Upvotes: 0

Views: 137

Answers (2)

Shivam
Shivam

Reputation: 720

You can't instantiate activity like this, Try this way,

public class ControlCenter1 extends AppCompatActivity {
    private static Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_control_center1);
        mContext = this;

public static Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg)
        {
            switch(msg.what)
            {
case GAME_OVER_START:
                {
                    CtlTip2 ctl = (CtlTip2) drawTip2.control;
                    ctl.init(ParrotsConstent.E_TIP.GAMEOVER.ordinal()); 
                    mSound.play(ParrotsConstent.E_SOUND.TIMEOVER);
                    break;
                }

    case GAME_OVER_END:
        {
          Intent intent = new Intent(mContext , Ads.class);
      mContext .startActivity(intent);
      break;
        }
}
}

Upvotes: 1

theblitz
theblitz

Reputation: 6881

You can't instantiate an Activity like that. It simply won't work.

You have to start it with startActivity.

Upvotes: 1

Related Questions