1

What's wrong? I am trying to extract a string used in strings.xml but I get error:

Error:(64, 72) error: non-static method getString(int) cannot be referenced from a static context

Code:

private static String file_url  = "http://xxxx.yy/" + getString(R.string.next_update_id);
diaconu liviu
  • 1,032
  • 2
  • 13
  • 30

3 Answers3

4

Create class MyApplication that extends Application. Implement singleton:

private static MyApplication instance;

public static MyApplication getInstance() {
     return instance;
}

public void onCreate() {
        instance = this;
}

Then use MyApplication.getInstance().getString(R.string.next_update_id)

Hope it helps!!!

Gueorgui Obregon
  • 5,077
  • 3
  • 33
  • 57
1

The message that you are receiving is already the answer: getString is a non-static method and you are trying to use it in a static context. Here "static context" means defining a static variable.

private static String file_url 

You could solve this issue by removing the static modifier from your member declaration.

Stefan Freitag
  • 3,578
  • 3
  • 26
  • 33
0

Simple fix

    private static String file_url;

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

        // Add only this
        file_url = "http://xxxx.yy/" + getString(R.string.next_update_id);
}
Fortran
  • 2,218
  • 2
  • 27
  • 33