简体   繁体   中英

java.lang.ExceptionInInitializerError Caused by: android.content.res.Resources$NotFoundException: String resource

I have a consts class:

public class AppConstants {
    public static final class Projects {
        public static final String JOB = Resources.getSystem().getString(R.string.job);
        // more...
    }
}

I want to use this consts in runtime:

if (AppConstants.Projects.JOB.equals(curr)) {
   // do stuff
}

But I get the following exception:

/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.ExceptionInInitializerError
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7f1101cc

How should I fix it? I think it happens because I use the keyword static . But how than I can access those fields?

JavaDoc of getSystem() says:

Return a global shared Resources object that provides access to only system resources (no application resources), is not configured for the current screen (can not use dimension units, does not change based on orientation, etc), and is not affected by Runtime Resource Overlay.

Is R.string.job a system resource or an application resource ?

JavaDoc also says:

You can generally acquire the Resources instance associated with your application with Context#getResources() .


R.string.job is not a system resource, so you need to access it through Context . You can than call context.getResources().getString(x) or directly context.getString(x) .

so why don't you just use this? (assuming you are already in a context, like an Activity or Application)

if (getString(R.string.job).equals(curr)) {
   // do stuff
}

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