Resource Not Found Exception in class without context or activity

3673 views android
-2

public class EmergencyNumbersGenerator {

    private static ArrayList<EmergencyNumberFeatures> features;

    public static ArrayList<EmergencyNumberFeatures> generateEmergencyNumber()
    {

        features = new ArrayList<>();

        EmergencyNumberFeatures em = new EmergencyNumberFeatures(
                Resources.getSystem().getString(R.string.policeControl),
                Resources.getSystem().getString(R.string.policeControlNumber)
        );
    }

}

This happens to be my code and this class EmergencyNumberGenerator has no Context whatsoever. So, I looked in SO for solution to get string from strings.xml and found that I can use

Resources.getSystem().getString(R.string.{stringID})

But, I am getting error where it says, Resource is not found.

 android.content.res.Resources$NotFoundException: String resource ID #0x7f0d0085
    at android.content.res.Resources.getText(Resources.java:348)
    at android.content.res.Resources.getString(Resources.java:441)
    at com.utilnepal.MobileHelp.data.EmergencyNumbersGenerator.generateEmergencyNumber(EmergencyNumbersGenerator.java:20)
    at com.utilnepal.MobileHelp.ActivitiesFragment.EmergencyFragment.onCreateView(EmergencyFragment.java:43)

Any help regarding this issue?

answered question

Can you pass context in this class ?? and you can use it like that context.getResources().getString();

I could. But I preferred not to. I am looking for way around!

Use shared preferences instead

1 Answer

3

So, Resources.getSystem() you can use everywhere in yours code.

But with that you can get only System Resources. String, that you defined in strings.xml is not system resource.

Check this link system resources and non-system

posted this

Have an answer?

JD

Please login first before posting an answer.