简体   繁体   中英

How to externalize the string for an error message in a custom exception class in Android

I have this class

public class InternetConnectionException extends RuntimeException {

    int code = 0;

    public InternetConnectionException(){
        super("Internet connection is down");
    }

    public InternetConnectionException(String message){
        super(message);
    }
}

This error message shouldn't be hardcoded. It should be externalized in order for different languages to be supplied, because when this exception is thrown the message is being displayed on the screen. Now, is there a way to externalize it as a strings.xml resource or should I change the design? (ie when this exception is thrown, just reference the string resource in the activity and display the resolved value)

I believe that a good design shouldn't allow pure Java classes such as an exception to know about the insides of Android framework, but I could be wrong.

Since you can't access string.xml directly but there is one way I can think of to achieve what you're trying to accomplish.

  • Put your all String values into string.xml
  • Maintain another java class(say, StringConstant.java) which will hold some of the contents of string.xml that you need to access in other non-Activity/Fragment classes.
  • Extend Application class(say, AppApplication.java) and initiate an object of StringConstant class here
  • Load the Strings from StringConstant class
  • Access the Strings from other class through the instance of StringConstant in Application class.

Here are some codes to demonstrate the idea

AppApplication.java

public class AppApplication extends Application {

    public static StringConstant STRING_CONSTANT;

    @Override
    public void onCreate() {
        super.onCreate();

        STRING_CONSTANT = new StringConstant(getBaseContext());
        STRING_CONSTANT.build();
    }
}

StringConstant.java

public class StringConstant {

    private Context context;

    public static String appName;
    // declare other String variables


    public StringConstant(Context context) {
        this.context = context;
    }

    public void build(){
        setAppName(context.getString(R.string.app_name));
        // set other String variables
    }

    public static String getAppName() {
        return appName;
    }

    public static void setAppName(String appName) {
        StringConstant.appName = appName;
    }
}

and now accessing the strings from another class

public class InternetConnectionException extends RuntimeException {

    int code = 0;

    public InternetConnectionException(){
        super(AppAplication.STRING_CONSTANT.getAppName());
    }

    public InternetConnectionException(String message){
        super(message);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM