android - I am getting a java.lang.NullPointerException by using setText() on TextView -


this question has answer here:

public class mainactivity extends activity{   textview datumtext; gesturedetector gesturedetector;   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      // gestures     datumtext = (textview) findviewbyid(r.id.datumtext);     datumtext.settext("ma");     gesturedetector = new gesturedetector(this, new gesturelistener());      datumtext.setontouchlistener(new view.ontouchlistener() {         @override         public boolean ontouch(view v, motionevent event) {             gesturedetector.ontouchevent(event);             return true;         }     }); } }  class gesturelistener implements gesturedetector.ongesturelistener{  mainactivity mainactivity = new mainactivity();  public gesturelistener(){  }  @override public boolean ondown(motionevent e) {     return false; }  @override public void onshowpress(motionevent e) {  }  @override public boolean onsingletapup(motionevent e) {     return false; }  @override public boolean onscroll(motionevent e1, motionevent e2, float distancex, float distancey) {     return false; }  @override public void onlongpress(motionevent e) {  }  @override public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) {     mainactivity.datumtext.settext("di");     return true; } } 

i want change text on

textview datumtext; 

whenever onfling motion on textview on-screen. getting nullpointerexception , think because trying call settext() method on null object.

thanks in advance!

edit: forgot mention use mainactivity class , second class. edit2: know nullpointerexception caused don't know how solve it.

you creating new instance of mainactivity , without inflating layout that, trying access textview don't exist in newly created mainactivity instance. change onfling() this

  @override public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) {     datumtext.settext("di"); // don't need create instance of mainactivity access textview     return true; } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -