Cannot make a static reference to the non-static method

后端 未结 7 2163
长发绾君心
长发绾君心 2020-11-21 04:32

Building a multi-language application in Java. Getting an error when inserting String value from R.string resource XML file:

public static final         


        
7条回答
  •  猫巷女王i
    2020-11-21 04:59

    There are some good answers already with explanations of why the mixture of the non-static Context method getText() can't be used with your static final String.

    A good question to ask is: why do you want to do this? You are attempting to load a String from your strings resource, and populate its value into a public static field. I assume that this is so that some of your other classes can access it? If so, there is no need to do this. Instead pass a Context into your other classes and call context.getText(R.string.TTT) from within them.

    public class NonActivity {
    
        public static void doStuff(Context context) {
            String TTT = context.getText(R.string.TTT);
            ...
        }
    }
    

    And to call this from your Activity:

    NonActivity.doStuff(this);
    

    This will allow you to access your String resource without needing to use a public static field.

提交回复
热议问题