vesii
vesii

Reputation: 3128

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?

Upvotes: 0

Views: 357

Answers (1)

Hawk
Hawk

Reputation: 2142

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
}

Upvotes: 1

Related Questions