-1

Before I ask, I've already looked at this post and tried all suggestions to no avail. My problem is that I can easily set a String using

getResources().getString(R.string.example)

within the onCreate method, but I want to set a public static final String to a value in string.xml using the same getResources() method. But, when I do this, I get a "Cannot make a static reference to the non-static method getResources() from the type ContextThemeWrapper". So I tried instantiating ContextThemeWrapper and also

this.getResources().getString(R.string.example)

which both clear the error, but both crash with a NullPointerException. Any solution to simply set a String from a resource would be much appreciated.

So far I have this:

public class MainActivity extends Activity {

    ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper();
    public final String CYPHER = contextThemeWrapper.getResources().getString(R.string.cypher_txt);
    public static final int LAYERS = 7;
    public static final int FLIP = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String appName = getResources().getString(R.string.app_name);
        Toast.makeText(this, "Welcome to " + appName, Toast.LENGTH_SHORT).show();

    }
Community
  • 1
  • 1
TheFreddyKilo
  • 417
  • 5
  • 10

1 Answers1

1

What you need is a static Context object. For that, you can use the application context.

Assuming your application manifest is as follows:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="my.app.package" ...

      <application android:name=".MyApp" ...

1) Create an application class with a public static method to return this context.

package my.app.package;  // This should match the package path in your Manifest

public class MyApp extends android.app.Application { // This should match the app name in your manifest

    private static MyApp appContext;

    public MyApp() { appContext = this; }

    public static Context getAppContext() { return appContext; }

}

2) Then in your activity, you can make statically obtain the context and resolve the resource:

public final String CYPHER = MyApp.getContext().getResources.getString(R.string.cypher_txt);
EJK
  • 12,332
  • 3
  • 38
  • 55
  • should be `MyApp extends Application`, the only thing that won't work is switching the device locale when there are multiple language versions of the string since it is loaded just once but never updates. – zapl Dec 02 '14 at 03:31
  • So does this approach fix your originally stated question? Dealing with multiple languages is a different concern that was not in your question. – EJK Dec 02 '14 at 04:03
  • As for switching the device locale... the user will have to restart the app in order for string resources to be resolved in the new locale. – EJK Dec 02 '14 at 04:04
  • Unfortunately, this does not work for me either. I came to the conclusion to just hard-code the string into the java code. Although I would really like to know how to make this work for future reference, even though I'm sure there are more than a few work-arounds for this. – TheFreddyKilo Dec 03 '14 at 01:20
  • I cannot see why this would not work. Any android.app.Context object can get you access to the application resources. What was your specific problem? – EJK Dec 03 '14 at 16:12
  • FWIW, this is a common approach. I use it in my app without any issues. – EJK Dec 03 '14 at 16:12