How do I get a string from string.xml in Java using Android SDK? -


before ask, i've looked @ this post , tried suggestions no avail. problem can set string using

getresources().getstring(r.string.example) 

within oncreate method, want set public static final string value in string.xml using same getresources() method. but, when this, "cannot make static reference non-static method getresources() type contextthemewrapper". tried instantiating contextthemewrapper , also

this.getresources().getstring(r.string.example) 

which both clear error, both crash nullpointerexception. solution set string resource appreciated.

so far 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 " + appname, toast.length_short).show();      } 

what need static context object. that, can use application context.

assuming application manifest follows:

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

1) create application class public static method return context.

package my.app.package;  // should match package path in manifest  public class myapp extends android.app.application { // should match app name in manifest      private static myapp appcontext;      public myapp() { appcontext = this; }      public static context getappcontext() { return appcontext; }  } 

2) in activity, can make statically obtain context , resolve resource:

public final string cypher = myapp.getcontext().getresources.getstring(r.string.cypher_txt); 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -