java - Android SlidingMenu nullpointer on toggle -


i've implemented slidingmenu, set slidingmenu sm = getslidingmenu() in activity want use menu. problem i've got want toggle via button i've created on canvas. if click these area on canvas stop drawing , open sliding menu - works until here. added content linearlayout , textview in it, on clicking textview text changes (kind of menu change options). , after changed once next click should toggle sliding menu again. i've created behind view's in class, , triggered method in activity containing slidingmenu, looks this:

    public void togglesm() {     thread splash = new thread(new runnable() {          @suppresswarnings("static-access")         @override         public void run() {             try {                 thread.currentthread().sleep(500);                 getslidingmenu().toggle(true);                 thread.currentthread().sleep(1000);                 animatebutton.setleftstate(false);             } catch (interruptedexception e) {                 e.printstacktrace();             }          }     });     splash.start(); } 

i added sleep avoid problems caused sm animation or changing content, aninamtebutton trigger drawing of canvas again. throws nullpointer exception. , without animation of slidingmenu.

edit:

the 2 classes i'm using:

public class ingame extends slidingactivity implements ontouchlistener{  private leftmenu lm; private slidingmenu sm;  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     sh = new screenhandler(this);     fh = new fileshandler(this);     animatemap = new animatemap(this);     ch = new coordshandler(this);     animatebutton = new animatebutton();     sched = new schedule();     lm = new leftmenu(this);      this.setrequestedorientation(activityinfo.screen_orientation_landscape);         this.requestwindowfeature(window.feature_no_title);     this.getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen);      setupviews();      lm.setupleftmenu();     setbehindcontentview(lm.getlayout());     setupslidingmenu(); }  @override protected void onpause() {     super.onpause();     gb.pause(); } @override protected void onresume() {     super.onresume();     gb.resume(); }  @suppresswarnings("deprecation") public void setupviews() {     framelayout fl = new framelayout(getbasecontext());     layoutparams sizep = new layoutparams(layoutparams.match_parent, layoutparams.match_parent);     fl.setlayoutparams(sizep);     fl.setbackgroundcolor(color.rgb(0, 153, 204));      gb = new gameboard(getbasecontext());     layoutparams fillp = new layoutparams(layoutparams.fill_parent, layoutparams.fill_parent);     gb.setlayoutparams(fillp);            gb.setontouchlistener(ingame.this);      fl.addview(gb);     setcontentview(fl);  }  public void setupslidingmenu() {     sm = getslidingmenu();     sm.settouchmodeabove(slidingmenu.touchmode_none);     sm.setmenu(lm.getlayout()); }  @suppresswarnings("static-access") @override public boolean ontouch(view v, motionevent e) {     float distx, disty;     switch (e.getaction()) {         case motionevent.action_down:             startx = e.getx();             starty = e.gety();             gb.moveddist.put("startx", startx);             gb.moveddist.put("starty", starty);             finx = finy = -1;             gb.moveddist.put("finx", finx);             gb.moveddist.put("finy", finy);             break;         case motionevent.action_move:             finx = e.getx();             finy = e.gety();             distx = startx - finx;             disty = starty - finy;             if(!checkunvalidvalues() && (math.abs(distx) > sh.gettsize() || math.abs(disty) > sh.gettsize())) {                 int rectdistx = (int) (distx/sh.gettsize());                 int rectdisty = (int) (disty/sh.gettsize());                 nomove = false;                 if(!checkedges(rectdistx, rectdisty)) {                     animatemap.animatexydistance(rectdistx, rectdisty);                     startx = e.getx();                     starty = e.gety();                 }             }             break;         case motionevent.action_up:             finx = e.getx();             finy = e.gety();             if(checkbutton() == ingamebutton.pp) {                 animatebutton.changeppstate();                       break;             }             if(checkbutton() == ingamebutton.left) { //where trigger slidingmenu                 animatebutton.setleftstate(true);                 if(!animatebutton.getppstate())                     animatebutton.setppstate(true);                 sm.toggle(true);                 lm.openleftmenu();                 break;             }              if(nomove && !checkunvalidvalues())                 animatemap.tellcoordinate(finx, finy);             nomove = true;             break;     }     return true; }  private boolean checkunvalidvalues() {     if(startx <= sh.getsidesize()-1 || startx >= (sh.getscreenwidth()-sh.getsidesize()))         return true;     if(starty <= -1 || starty >= (sh.getscreenheight()-sh.getbottomguisize()))         return true;     if(finx <= sh.getsidesize()-1 || finx >= (sh.getscreenwidth()-sh.getsidesize()))         return true;     if(finy <= -1 || finy >= (sh.getscreenheight()-sh.getbottomguisize()))         return true;     return false; }  public slidingmenu getsm() {     return sm; }  public void togglesm() {      runonuithread(new runnable() {          @suppresswarnings("static-access")         @override         public void run(){                  getslidingmenu().toggle(true);                 slidingmenu sm = getslidingmenu();                 sm.getalpha();             try {                 thread.currentthread().sleep(1000);                 animatebutton.setleftstate(false);             } catch(interruptedexception e) {                 e.printstacktrace();             }          }      }); } 

leftmenu:

public class leftmenu {  private context context; private ingame ingame; private animatebutton animatebutton;  private linearlayout ll; private linearlayout.layoutparams llp; private static leftmenupage page; private static int buttontextsize = 15;  public leftmenu(context context) {     this.context = context;     ingame = new ingame();       animatebutton = new animatebutton(); }  public void setupleftmenu() {     page = leftmenupage.overview;     setuplinearlayout();     setupbuttons(); }  @suppresswarnings("deprecation") private void setuplinearlayout() {     ll = new linearlayout(context);     ll.setorientation(linearlayout.vertical);     ll.setbackgroundcolor(0xff000000);     llp = new linearlayout.layoutparams(         linearlayout.layoutparams.fill_parent,         linearlayout.layoutparams.fill_parent);     ll.setlayoutparams(llp); } public linearlayout getlayout() {     return ll; }  private void setupbuttons() {     final textview t1 = new textview(context);     t1.setbackgroundcolor(0xff000000);     t1.settextcolor(0xffffffff);     t1.settypeface(null, typeface.normal);     t1.settextsize(buttontextsize);     linearlayout.layoutparams lp = new linearlayout.layoutparams(         linearlayout.layoutparams.wrap_content,         linearlayout.layoutparams.wrap_content);     lp.weight = 1.0f;     lp.gravity=1;     t1.setlayoutparams(lp);      string content = null;     if(page == leftmenupage.overview) {         content = "creation mode";     }     else {         content = "back";     }     t1.settext(content);     ll.addview(t1);      t1.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             t1.settextcolor(0xffcccccc);             switch(page) {                 case overview:                           page = leftmenupage.create;                     t1.settext("create dwarves");                      ingame.togglesm();                     if(!animatebutton.getppstate())                         animatebutton.setppstate(true);                      break;                 case create:                     ingame.togglesm();                     if(!animatebutton.getppstate())                         animatebutton.setppstate(true);                     break;             }             t1.settextcolor(0xffffffff);         }     }); }  public void openleftmenu() {     page = leftmenupage.overview; } 

}

edit 2:

    05-10 15:14:53.535: e/androidruntime(25064): fatal exception: main 05-10 15:14:53.535: e/androidruntime(25064): java.lang.nullpointerexception 05-10 15:14:53.535: e/androidruntime(25064):    @ com.jeremyfeinstein.slidingmenu.lib.app.slidingactivity.getslidingmenu(slidingactivity.java:104) 05-10 15:14:53.535: e/androidruntime(25064):    @ me.g4mem0ment.dwarvenskill.ingame$1.run(ingame.java:234) 05-10 15:14:53.535: e/androidruntime(25064):    @ android.os.handler.handlecallback(handler.java:615) 05-10 15:14:53.535: e/androidruntime(25064):    @ android.os.handler.dispatchmessage(handler.java:92) 05-10 15:14:53.535: e/androidruntime(25064):    @ android.os.looper.loop(looper.java:137) 05-10 15:14:53.535: e/androidruntime(25064):    @ android.app.activitythread.main(activitythread.java:4918) 05-10 15:14:53.535: e/androidruntime(25064):    @ java.lang.reflect.method.invokenative(native method) 05-10 15:14:53.535: e/androidruntime(25064):    @ java.lang.reflect.method.invoke(method.java:511) 05-10 15:14:53.535: e/androidruntime(25064):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:994) 05-10 15:14:53.535: e/androidruntime(25064):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:761) 05-10 15:14:53.535: e/androidruntime(25064):    @ dalvik.system.nativestart.main(native method) 

stacktrace, mentionen in comments, getslidingmenu returns null , mhelper (slidingactivityhelper) causes because it's null

getslidingmenu().toggle(true); 

has run on ui thread. move inside , handler or if inside activity use

 runonuithread(new runnable() {       public void run(){           getslidingmenu().toggle(true);       }   }); 

Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -