The final variable from the resource file

I have activity with some final variables . I extracted their values ​​(suppose they are all strings) into a resource file.

Problem:

If I directly assign them to an instance (as follows):

private final String PREFERENCE_NAME = getResources().getString(R.string.preference_name);

I get the following error:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

I understand the problem; the onCreate () method has not yet been called, which means that I cannot access the context related methods ( getResources()).

If I want to assign a value in the onCreate () method of an activity, I get an error Cannot assign value to final variable 'PREFERENCE_NAME'

Question . How can I get my final variables from a resource file? And if this is not possible, what is the best practice to solve?

Thanks in advance.

+4
5

, , . , getResources() .

+3

: .

final (.. ).

getResources().getString(R.string.preference_name); .

, , .

final, VM . , . , .

:

public class FinalMessage {

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        FinalMessage f = new FinalMessage("Hello World!");
        System.out.println(f.getMessage());
        f.changeFinalMessage("Hello Mars!");
        System.out.println(f.getMessage());
    }

    private final String message;

    public FinalMessage(String message) {
        this.message = message;
    }

    void changeFinalMessage(String newMessage) throws IllegalAccessException, NoSuchFieldException {
        final Field field = FinalMessage.class.getDeclaredField("message");
        field.setAccessible(true);

        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

        field.set(this, newMessage);
    }

    String getMessage() {
        return message;
    }
}

:

!
, !

, . ?

, :

public class FinalMessage {

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        FinalMessage f = new FinalMessage();
        System.out.println(f.getMessage());
        f.changeFinalMessage("Hello Mars!");
        System.out.println(f.getMessage());
    }

    private final String message = "Hello World!";

    void changeFinalMessage(String newMessage) throws IllegalAccessException, NoSuchFieldException {
        final Field field = FinalMessage.class.getDeclaredField("message");
        field.setAccessible(true);

        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

        field.set(this, newMessage);
    }

    String getMessage() {
        return message;
    }
}

:

!
, !

, ?

, , message "Hello World!", "Hello World!" f.getMessage(). , , "Hello Mars!", , .

, : ( , Security Manager, ), , , .

, , .

+4

factory, .

- , , , - .

, , factory, final .

-

class MyFactory {
   private Resource getResources() { ... }
   public MyObject build() { 
       String perference_name = getResources().getString(R.string.preference_name);
       /...
       return new MyObject(perfence_name ,....);
   }
}
+1

:

  • :

    public class MyApplication extends Application {
    
        private static Context mContext;
    
        @Override
        public void onCreate() {
            super.onCreate();
            mContext = getApplicationContext();
        }
    
        public static String getStr(int resId) {
            return mContext.getString(resId);
        }
    
    }
    
  • :

    <application
        android:name=".MyApplication"
        ...
    
  • :

    static final String NAME = MyApplication.getStr(R.string.app_name);
    
+1

:

private final String PREFERENCE_NAME() {
    return getResources().getString(R.string.preference_name);}
0

Source: https://habr.com/ru/post/1585057/


All Articles