getString Outside of a Context or Activity

后端 未结 12 2088
遥遥无期
遥遥无期 2020-11-28 01:12

I\'ve found the R.string pretty awesome for keeping hardcoded strings out of my code, and I\'d like to keep using it in a utility class that works with models i

12条回答
  •  一生所求
    2020-11-28 01:31

    BTW, one of the reason of symbol not found error may be that your IDE imported android.R; class instead of yours one. Just change import android.R; to import your.namespace.R;

    So 2 basic things to get string visible in the different class:

    //make sure you are importing the right R class
    import your.namespace.R;
    
    //don't forget about the context
    public void some_method(Context context) {
       context.getString(R.string.YOUR_STRING);
    }
    

提交回复
热议问题