java - FindViewById cannot be referenced from static context -


i trying access method, changes text field in ui, of activity java file. in game.java (normal java file in background) have static variables changed on time. want changes in these variables reflected in actual ui. hence, trying access method "changename" in displaymessageactivity.java reflect corresponding changes (display_message_activity.xml).

this method in displaymessageactivity.java trying call game.java

public void changename() {     textview text = (textview) findviewbyid(r.id.petname);     text.settext("" + game.name); } 

to call displaymessageactivity.changename() game.java, have change static method.

public static void changename() {     textview text = (textview) findviewbyid(r.id.petname);     text.settext("" + game.name); } 

but doing gives me error "non-static method cannot accessed static context" "findviewbyid". tried making instance of displaymessageactivity.java in game.java access "changename" method without making static. throws nullpointerexception @ textview text = (textview) findviewbyid(r.id.petname);

how can resolve issue?

create static reference displaymessageactivity in , initialize in oncreate().

public class displaymessageactivity { displaymessageactivity instance;      @override     public void oncreate() {         ...         instance = this;         ...     }  } 

then game.java can access non-static method using:

displaymessageactivity.instance.changename(); 

edit: btw, npe being thrown because created new instance of displaymessageactivity not 1 using.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -