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

huangapple go评论75阅读模式
英文:

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 can I access those fields?

英文:

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?

答案1

得分: 1

Translated content:

JavaDoc of getSystem() says:

返回一个全局共享的 Resources 对象,仅提供对系统资源(不包括应用程序资源)的访问,未配置为当前屏幕(不能使用尺寸单位,不会根据方向更改等),并且不受运行时资源覆盖的影响。

R.string.job系统 资源还是 应用程序 资源?

JavaDoc 还提到:

您通常可以通过 Context#getResources() 获取与您的应用程序关联的 Resources 实例。


R.string.job 不是系统资源,因此您需要通过 Context 来访问它。然后可以调用 context.getResources().getString(x) 或直接使用 context.getString(x)

所以为什么不直接使用这个呢?(假设您已经处于上下文环境中,比如一个 Activity 或 Application)

if (getString(R.string.job).equals(curr)) {
   // 做一些操作
}
英文:

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
}

huangapple
  • 本文由 发表于 2020年8月12日 00:55:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63362861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定