0

Can anybody tell me why this isn't working? I want it to load a specific layout depending on what page is loaded. The if clause seems to be faulty. It's always going to the else.

     wordArray = getResources().getStringArray(resId);
     int resIdBack = getResources().getIdentifier(wordArray[5], "drawable", "com.bobgle.libretto");

    // If Background Image is dark use light theme, if not use dark theme.
    if(wordArray[5] == "libretto"){
        view = inflater.inflate(R.layout.activity_fragment_dark, container, false);
    } else {
        view = inflater.inflate(R.layout.activity_fragment_light, container, false);
    }
TemporaryName
  • 487
  • 5
  • 16
  • Also as a side note, I see that you have never accepted a answer on stackOverFlow, Please take your time to accept the answer that you think is correct by cross/tick it so you can help others with your question. Also it gives those who answers your question a reward for doing so. – Rawa Jul 30 '14 at 13:20
  • @Rawa: I know! But more than often I get the answer from the comments to my question rather than the actual answers, so I can't accept them (or it would be wrong because they didn't help me). – TemporaryName Jul 30 '14 at 13:23
  • Ok, good, thanks! Good luck with your app! – Rawa Jul 30 '14 at 13:24

1 Answers1

1

When comparing strings in Java you should use the equals method.

When using equal is good to remember that it can cause nullpointer if the object you are using equals on are null (duh!) so if you got a fixed string, that can't be null always use that first.

"libretto".equals(wordArray[5]) //Can't cause nullpointerexception.
wordArray[5].equals("libretto") //Can cause nullpointerexception if wordArray[5] is null.

This SO question explains why that is the case.

Community
  • 1
  • 1
Rawa
  • 13,357
  • 6
  • 39
  • 55