java - Android- cant get String from Resources for Static field- Non-static Method getResources cannot be referenced from a static-context" -
i passed images , text in grid view launchericons class. below codes, works fine. want change string as
new launchericon(r.mipmap.ic_launcher,getresources().getstring(r.string.hello))
i want string resources(r.string.hello) , when implement getresources warning "non-static method getresources cannot referenced static-context"
public class mainactivity extends appcompatactivity implements adapterview.onitemclicklistener { string activity="mainactivity"; context activity_context=mainactivity.this; static final launchericon[] icons = { new launchericon(r.mipmap.ic_launcher,"hello"), new launchericon(r.mipmap.ic_launcher, "about me"), new launchericon(r.mipmap.ic_launcher, "venky"), new launchericon(r.mipmap.ic_launcher, "noctilien"), new launchericon(r.mipmap.ic_launcher, "metro"), new launchericon(r.mipmap.ic_launcher, "rer"), new launchericon(r.mipmap.ic_launcher, "bus"), new launchericon(r.mipmap.ic_launcher, "metro"), new launchericon(r.mipmap.ic_launcher, "rer"), new launchericon(r.mipmap.ic_launcher, "bus"), }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //blah blah............. }
launchericon class
static class launchericon { final string text; final int imgid; //final string map; public launchericon(int imgid, string text) { super(); this.imgid = imgid; this.text = text; // this.map = map; } }
how use getresources string in there? in advance.
the things i'd change remove static
in launchericon
class, , :
public class launchericon { final string text; final int imgid; public launchericon(int imgid, string text) { super(); this.imgid = imgid; this.text = text; } }
then create array of launchericons
without static
follows :
launchericon[] icons = { new launchericon(r.mipmap.ic_launcher,"hello"), new launchericon(r.mipmap.ic_launcher, "about me"), new launchericon(r.mipmap.ic_launcher, "venky"), new launchericon(r.mipmap.ic_launcher, "noctilien"), new launchericon(r.mipmap.ic_launcher, "metro"), new launchericon(r.mipmap.ic_launcher, "rer"), new launchericon(r.mipmap.ic_launcher, "bus"), new launchericon(r.mipmap.ic_launcher, "metro"), new launchericon(r.mipmap.ic_launcher, "rer"), new launchericon(r.mipmap.ic_launcher, "bus"), };
i passed images , text in grid view launchericons class. below codes, works fine. want change string :
new launchericon(r.mipmap.ic_launcher,getresources().getstring(r.string.hello));
if error doing this, can call doing (the first way if change answer should work):
launchericon(r.mipmap.ic_launcher, yourcontext.getresources().getstring(r.string.hello));
Comments
Post a Comment