5

I have read few articles on trying to stop adView when the app is hidden/minimised but this crashes my app.

This is my code, adView.LoadAd... and adView.stopLoading both crashes the app on startup.

public class MainActivity extends Activity implements OnItemSelectedListener {
    @Override
    protected void onResume() {
        super.onResume();
        if (AdViewStarted = true) {
            adView.loadAd(new AdRequest());
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (AdViewStarted = true) {
            adView.destroy();
        }
    }

    [...]

    public class AdMob extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            adView = new AdView(this, AdSize.BANNER,"12345678901234567890");
            LinearLayout layout = (LinearLayout) findViewById(R.id.adView);
            layout.addView(adView);
            adView.loadAd(new AdRequest());
            AdViewStarted = true;
        }

        @Override
        public void onDestroy() {
            if (adView != null) {
                adView.destroy();
            }
            super.onDestroy();
        }
    }
}

Thanks in advance

Leandros
  • 16,805
  • 9
  • 69
  • 108
KickAss
  • 4,210
  • 8
  • 33
  • 41

1 Answers1

5

Replace the if statements, you have to use two equals instead of one. Correct would be

if (AdViewStarted == true) {
        adView.destroy();
}

or better

if (AdViewStarted) {
        adView.destroy();
}

By the win, variable names are starting with a lowercase char.

Also, what are you trying in your onCreate?

This is correct (I think, if not, show me the layout xml file and LogCat):

LinearLayout adView = (LinearLayout) findViewById(R.id.adView);
adView.loadAd(new AdRequest());
AdViewStarted = true;
Leandros
  • 16,805
  • 9
  • 69
  • 108
  • Oh wow. I can't believe I missed the == lol I keep doing that. I'm self teaching Java, the scripting language I used before only needed 1 = sign so I keep forgetting. Thanks :) – KickAss Apr 03 '13 at 18:31
  • I have nothing AdMob related in the onCreate() for the MainActivity. This is the xml: This is why the ads start automatically – KickAss Apr 03 '13 at 18:41
  • What about the Activity `AdMob`? – Leandros Apr 03 '13 at 18:46
  • What about it? I don't call it. It gets called automatically... somehow... I'm not sure. I added the AdMob activity to main activity. Modified layout in xml, added the packages to Manifest and linked the Google...jar file. That's it, and it works. – KickAss Apr 03 '13 at 18:49