0

I have interstitial ads working perfectly in my navigation drawer eg. home, apply, icons etc... but same home, apply, icons etc... tiles on my dashboard (homepage) always crashes my app whenever i click on any of them so i'm trying this method below:

    mNavigationView.setItemBackground(background);
    mNavigationView.setNavigationItemSelectedListener(item -> {
        int id = item.getItemId();
        if (id == R.id.navigation_view_home) mPosition = 0;
        else if (id == R.id.navigation_view_apply) mPosition = 1;
            interstitial = new InterstitialAd(getApplicationContext());
            interstitial.setAdUnitId(getString(R.string.admob_interstitial_ad));
            AdRequest adRequest = new AdRequest.Builder().build();
            interstitial.loadAd(adRequest);
            interstitial.setAdListener(new AdListener() {
                public void onAdLoaded() {
                    if (interstitial.isLoaded()) {
                        interstitial.show();
                    }
                }
            });

        else if (id == R.id.navigation_view_icons) mPosition = 2;
        else if (id == R.id.navigation_view_request) mPosition = 3;
        else if (id == R.id.navigation_view_wallpapers) mPosition = 4;
        else if (id == R.id.navigation_view_settings) mPosition = 5;
        else if (id == R.id.navigation_view_faqs) mPosition = 6;
        else if (id == R.id.navigation_view_about) mPosition = 7;

        item.setChecked(true);
        mDrawerLayout.closeDrawers();
        return true;
    });

    NavigationViewHelper.hideScrollBar(mNavigationView);
}

but always get Error:(623, 13) error: 'else' without 'if' any help would be appreciated, Thank u

Ronald
  • 9
  • 1
    Java is **not** Python etc., thus, multiline code in `if-else` has to be put into brackets -> the first `else if` has multiple lines of Java code and needs to be put into `{... }` – UninformedUser Sep 17 '17 at 21:08
  • 2
    Possible duplicate of [Else without if error?](https://stackoverflow.com/questions/24210293/else-without-if-error) – Tom Sep 17 '17 at 21:09

1 Answers1

0

if you have an expression inside of your if or else clause with more than one line - you should wrap it with {..}. since you don't, your if-else ends after one line only.

if (id == R.id.navigation_view_home) mPosition = 0;
        else if (id == R.id.navigation_view_apply) mPosition = 1; // your else ends here
            interstitial = new InterstitialAd(getApplicationContext());
            interstitial.setAdUnitId(getString(R.string.admob_interstitial_ad));
            AdRequest adRequest = new AdRequest.Builder().build();
            interstitial.loadAd(adRequest);
            interstitial.setAdListener(new AdListener() {
                public void onAdLoaded() {
                    if (interstitial.isLoaded()) {
                        interstitial.show();
                    }
                }
            });

As a good practice, I prefer to always do it, also for one line expressions

Nir Levy
  • 12,750
  • 3
  • 21
  • 38