java - call method from non-Activity class android -


i have popup window (non-activity class) have buttons. 1 of them (btn_audio), play or pause sound , works charm. want call 2 method, play() , pause() in homeactivity , inside of onstop() method in of activity's. doesn't work, call play() , pause() in other activity's occur nullpointexeption! help...

public class popup_menu extends activity implements onclicklistener {   button btn_audio;   public static mediaplayer player;     string play_or_pause;   int num_ply, tim_pos; int [] resid;// audio   sharedpreferences sp; void showpopup(final activity context) {    this.context=context;   linearlayout viewgroup = (linearlayout) context.findviewbyid(r.id.popup);        layoutinflater layoutinflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service);        layout = layoutinflater.inflate(r.layout.popup_layout_menu, viewgroup);   sp = context.getsharedpreferences("text", mode_private);        btn_audio = (button) layout.findviewbyid(r.id.btn_audio);    // audio         layout.findviewbyid(r.id.btn_audio).setonclicklistener(this);           @override         public void onclick(view whichbuttonisclicked) {   switch (whichbuttonisclicked.getid())  {   case r.id.btn_audio:                           if (play_or_pause.equals("play")) {        pause();   } else {       play(context);   }   sp.edit().putstring("play_or_pause", play_or_pause).commit();   // save current stat }   ////// close of showpopup()            //define play , pause methods    public void pause() {   player.pause();   btn_audio.setbackgroundresource(r.drawable.icon_audio_pause);   play_or_pause="pause"; // current state 'pause'             }  public void play(final context context) {   if(num_ply==9){num_ply=0; }   resid = new int []{ r.raw.tarane,r.raw.toofan,r.raw.shane,r.raw.kharazmi,r.raw.golhaye_khofte,r.raw.rang,r.raw.naghmeh,r.raw.dar_rahe_to,r.raw.emperor};   player=mediaplayer.create(context,resid[num_ply]);   player.seekto(tim_pos);              player.start();   btn_audio.setbackgroundresource(r.drawable.icon_audio_play);   play_or_pause="play";      // current state 'play'   player.setoncompletionlistener(new oncompletionlistener() {   public void oncompletion(mediaplayer player) {   tim_pos=0;            num_ply++;      play(context);           }       });   }   } 

and homeactivity :

public class homeactivity extends activity implements onclicklistener {  popup_menu ppp;  @override protected void oncreate..............  ppp = new popup_menu();  sharedpreferences sp = getsharedpreferences("text", mode_private);   if(sp.getstring("play_or_pause", "play").equals("pause")){    ppp.play(getapplicationcontext());                 // error     }    // audio  }  /// close of oncreate  @override public void onstop() {   super.onstop();                                  if(play_or_pause.equals("play")){   ppp.tim_pos= ppp.player.getcurrentp 

ok, decided use service resolve problem , works play(). when playing, still can't call pause() method soundservice because player object null. think creating object, soundservice class, (ss in popup_menu class) , default constructor put player value null (when playing , player object wasn't null). , calling player.pause occur error. how please can pause one_week_friend!!!

public class soundservice extends service {      mediaplayer player;     int num_ply, tim_pos;   int [] resid;   public int onstartcommand(intent intent, int flags, int startid) {      resid = new int []{ r.raw.tarane,r.raw.toofan,r.raw.shane,r.raw.kharazmi,             r.raw.golhaye_khofte,r.raw.rang,r.raw.naghmeh,r.raw.dar_rahe_to,r.raw.emperor     };         player=mediaplayer.create(this,resid[num_ply]);          play();         return service.start_flag_redelivery;  }  public void play() {     if(num_ply==9){num_ply=0; }     player.seekto(tim_pos);              player.start();     player.setoncompletionlistener(new oncompletionlistener() {              public void oncompletion(mediaplayer player) {         tim_pos=0;          num_ply++;      play();     }     }); }      public void resume(){     player.seekto(tim_pos);              player.start(); }  public void pause() {     tim_pos= player.getcurrentposition();          ////// error     player.pause();          ////// error     num_ply++; }   public ibinder onbind(intent arg0) {     // todo auto-generated method stub     return null;   } } 

and popup_menu class:

@override public void onclick(view whichbuttonisclicked) {     switch (whichbuttonisclicked.getid())  {      case r.id.btn_audio:     if (play_or_pause.equals("play")) {         btn_audio.setbackgroundresource(r.drawable.icon_audio_pause);         play_or_pause="pause";       // current state 'pause'         soundservice ss = new soundservice();         ss.pause();          ////// error     } else {         btn_audio.setbackgroundresource(r.drawable.icon_audio_play);         play_or_pause="play";        // current state 'play'         intent sound_intent = new intent(context , soundservice.class);     // start service         context.startservice(sound_intent);     }         sp.edit().putstring("play_or_pause", play_or_pause).commit();   // save current state     break; 

logcat:

08-05 01:03:31.440: e/androidruntime(25356): fatal exception: main 08-05 01:03:31.440: e/androidruntime(25356): java.lang.nullpointerexception 08-05 01:03:31.440: e/androidruntime(25356):    @ com.codegostarniloo.negar.soundservice.pause(soundservice.java:47) 08-05 01:03:31.440: e/androidruntime(25356):    @ com.codegostarniloo.negar.popup_menu.onclick(popup_menu.java:234) 08-05 01:03:31.440: e/androidruntime(25356):    @ android.view.view.performclick(view.java:4209) 08-05 01:03:31.440: e/androidruntime(25356):    @ android.view.view$performclick.run(view.java:17457) 08-05 01:03:31.440: e/androidruntime(25356):    @ android.os.handler.handlecallback(handler.java:725) 08-05 01:03:31.440: e/androidruntime(25356):    @ android.os.handler.dispatchmessage(handler.java:92) 08-05 01:03:31.440: e/androidruntime(25356):    @ android.os.looper.loop(looper.java:153) 08-05 01:03:31.440: e/androidruntime(25356):    @ android.app.activitythread.main(activitythread.java:5341) 08-05 01:03:31.440: e/androidruntime(25356):    @ java.lang.reflect.method.invokenative(native method) 08-05 01:03:31.440: e/androidruntime(25356):    @ java.lang.reflect.method.invoke(method.java:511) 08-05 01:03:31.440: e/androidruntime(25356):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:929) 08-05 01:03:31.440: e/androidruntime(25356):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:696) 08-05 01:03:31.440: e/androidruntime(25356):    @ dalvik.system.nativestart.main(native method) 08-05 01:03:32.066: d/dalvikvm(25356): threadid=11: interp stack @ 0x5f1c9000 

thank's lot...

first, off, don't extend activity if it's non activity, claim, passing context preferred on activity

secondly, there class called dialogfragment intended used popup dialog

i think looking following, though

public class popup_menu implements onclicklistener {     button btn_audio;     public mediaplayer player;          string play_or_pause;     int num_ply, tim_pos;      int [] resid;// audio     sharedpreferences sp;     context context ;      public popup_menu(final context context) {          this.context= context;         sp = this.context.getsharedpreferences("text", context.mode_private);         // linearlayout viewgroup = (linearlayout)  view.findviewbyid(r.id.popup);          layoutinflater layoutinflater = layoutinflater.from(this.context);         layout = layoutinflater.inflate(r.layout.popup_layout_menu, viewgroup);         btn_audio = (button) layout.findviewbyid(r.id.btn_audio);    // audio         btn_audio.setonclicklistener(this); }  

you can create new popup_menu(homeactivity.this)


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -