0

I'm having a java syntax error on IF ELSE statement. I reviewed my code several times but couldn't figure out what's wrong. The Error is : Syntax error on token "else", delete this token. What am I doing wrong here?

if(androidFragment!=null)
    ft.detach(androidFragment);

if(appleFragment!=null)
    ft.detach(appleFragment);

if(berryFragment!=null)
    ft.detach(berryFragment);


if(tabId.equalsIgnoreCase("android")){

    if(androidFragment==null){      

        ft.add(R.id.realtabcontent,new AndroidFragment(), "android");                       
    }else{

        ft.attach(androidFragment);                     
    }

}else if{   

    if(appleFragment==null){

        ft.add(R.id.realtabcontent,new AppleFragment(), "apple");                       
    }else{

        ft.attach(appleFragment);               
    }

}else{  

    if(berryFragment==null){

        ft.add(R.id.realtabcontent,new BerryFragment(), "berry");                       
    }else{

        ft.attach(berryFragment);                       
    }

}
2
  • Which line gives the error? Commented Apr 5, 2013 at 18:07
  • Use the auto-format command of your IDE, Commented Apr 5, 2013 at 18:11

6 Answers 6

3

You have no condition in this if

...
}else if{ /* condition missing at this if */  

    if(appleFragment==null){

        ft.add(R.id.realtabcontent,new AppleFragment(), "apple");                       
    }else{

        ft.attach(appleFragment);               
    }
}...

Change it to what you need, possibly:

...
}else if (tabId.equalsIgnoreCase("apple")){

    if(appleFragment==null){

        ft.add(R.id.realtabcontent,new AppleFragment(), "apple");                       
    }else{

        ft.attach(appleFragment);               
    }
}...
Sign up to request clarification or add additional context in comments.

1 Comment

This is the correct answer to your problem. Please accept it as such.
1

You can only have one else attached to an if; here you have two elses, for AppleFragment and BerryFragment.

if(tabId.equalsIgnoreCase("android")){

  ...
}else{  

    ...

}else{  

    ...

}

EDIT

You now have the following code fragment:

} else if {

Your else if requires a condition, e.g.

} else if (/*another boolean condition here for AppleFragment*/) {

2 Comments

I saw your question edit. I have edited my answer; you need a condition on your else if.
@Downvoter - The OP originally had } else { but later changed it to } else if {. If that's not the reason, then why did you downvote?
1

You have 2 else's in a row

if (...) {
   ...
} else {
   ...
} else {   <--only one "else" allowed.
   ...
}

4 Comments

He has an 'if' after the first else. His issue isn't the two elses, but rather the 'if' having no condition.
I was wondering if the post had been edited, but I saw no evidence of it. Nor did I know you even could edit your post without a "Last Editted" stamp appearing on it.
there's a period where the edit flag doesn't come up. not sure what it is, but it lets the question be updated without counting as an edit.
@MarcB Fix your answer. Don't just leave it like that.
1

You didn't use the else if construct properly, you code should look like :

}else if(tabId.equalsIgnoreCase("apple")) {   

    if(appleFragment==null){
        ...

}else if (tabId.equalsIgnoreCase("berry")){  

    if(berryFragment==null){
        ...
}

Comments

0

Just one else is allowed, use else if

if(condition){ ....

}else if(condition 2){ ......

}else{ ........

}

Comments

0

The problem is here :

else{   // Second else compilation error

    if(berryFragment==null){

        ft.add(R.id.realtabcontent,new BerryFragment(), "berry");                       
    }else{

        ft.attach(berryFragment);                       
    }

}

You cannot have two else statements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.