0

I looked here to try and solve my problem. It is similar but I use fragments so when I need context I usually need to call getActivity().

Basically I have App.java as described in the link above, I have

android:name=".App" inside my <application> tag

added to my AndroidManifest.xml. Now I have this class I use to collect all the things i use frequently:

public class MiscMethods{
public static void ErrorToast(int errorCode) {
    String errorString = null;
    if(errorCode==1){ errorString = App.getContext().getString(R.string.error_tooManyFieldsEmpty);}
    if(errorCode==2){ errorString = App.getContext().getString(R.string.error_featureComingSoon);}
    if(errorCode==3){ errorString = App.getContext().getString(R.string.error_SwitchBreak);}
    else{errorString="Wrong Error Code";}
    Toast errormsg = Toast.makeText(App.getContext(), errorString, Toast.LENGTH_SHORT);
    errormsg.setGravity(Gravity.CENTER, 0, 0);
    errormsg.show();
}
}

In one of my fragments I call

MiscMethods.ErrorToast(1);

I just get the "Wrong Error Code" message from the "else {}" part of my method

Can you help me make this right?

Community
  • 1
  • 1
Killerpixler
  • 4,200
  • 11
  • 42
  • 82

2 Answers2

1

better formatting would have made your issue easier to find:

public static void ErrorToast(int errorCode) {
    String errorString = null;
    if (errorCode == 1) {
        errorString = App.getContext().getString(R.string.error_tooManyFieldsEmpty);
    }
    if (errorCode == 2) {
        errorString = App.getContext().getString(R.string.error_featureComingSoon);
    }
    if (errorCode == 3) {
        errorString = App.getContext().getString(R.string.error_SwitchBreak);
    } else {
        errorString = "Wrong Error Code";
    }
    Toast errormsg = Toast.makeText(App.getContext(), errorString, Toast.LENGTH_SHORT);
    errormsg.setGravity(Gravity.CENTER, 0, 0);
    errormsg.show();
}

As you might now see is that your if (errorCode == 1) should work but will be overridden as if (errorCode == 3) will be false in this case and your else will override your errorString variable.

A switch(errorCode) and the 3 cases are what you are looking for.

Final tip: improve your formatting!

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
  • Thanks, wasn't a formatting issue, i just didn't understand that the else goes as soon as the last before it is false... i thought it only goes if all of the above are false :) made a switch now – Killerpixler Oct 02 '12 at 22:36
  • I just meant that with a better formatting, you should have seen the program flow easier :) You read the code 10 times more than you write, so good readability is always worth the "more work". – WarrenFaith Oct 05 '12 at 08:31
0

write under code in Your CoreApplication.java

[step1]

public class CoreApplication extends Application {

private static CoreApplication instance; 
}

[step2]

onCreate(){
instance = this;
}

[step3]
add

public static CoreApplication getGlobalApplicationContext() {

if (instance == null) {
throw new IllegalStateException("this application does not 
inherit GlobalApplication"); " +
"}

return instance;
}

[Step4]
Call getGlobalApplicationContext() in your fragment

Giulio Caccin
  • 2,962
  • 6
  • 36
  • 57
aiden
  • 1
  • 3