java - NullPointerException occured while using Binder.transact()? -


this question has answer here:

here mainactivty discard method pasted here makes code shorter u guys

public class mainactivity extends activity {  private button btn1,btn2; private binder binder; @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);      setcontentview(r.layout.activity_main);     btn1 = (button)findviewbyid(r.id.btn1);     btn2 = (button)findviewbyid(r.id.btn2);     btn1.setonclicklistener(new onclicklistener() {          @override         public void onclick(view v) {             // todo auto-generated method stub             intent intent = new intent();             intent.setclass(mainactivity.this, secondservice.class);             bindservice(intent, connection, bind_auto_create);         }     });      btn2.setonclicklistener(new onclicklistener() {          @override         public void onclick(view arg0) {             // todo auto-generated method stub              parcel data = parcel.obtain();             parcel reply = parcel.obtain();              data.writestring("from activity: data");              try {                 //something wrong here?                 binder.transact(0, data, reply, 0);                  string string = reply.readstring();                  system.out.println("actvity   --->"+string);             } catch (remoteexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }         }     }); } serviceconnection connection = new serviceconnection() {       @override     public void onservicedisconnected(componentname componentname) {         // todo auto-generated method stub      }      @override     public void onserviceconnected(componentname componentname, ibinder binder) {         // todo auto-generated method stub          mainactivity.this.binder = (binder)binder;     } }; 

and here's service

public class secondservice extends service{  @override public ibinder onbind(intent intent) {     // todo auto-generated method stub      return  new mybinder(); }  class mybinder extends binder{     @override     protected boolean ontransact(int code, parcel data, parcel reply,             int flags) throws remoteexception {         // todo auto-generated method stub          system.out.println("code---->"+code);          string string = data.readstring();         system.out.println("s---->"+string);          reply.writestring("reply :-->service");         return super.ontransact(code, data, reply, flags);     } }} 

logcat below: enter image description here


it says line55: binder.transact(0, data, reply, 0); has problems dont know why

any answer appreciate !

please write code in oncreate

 intent intent = new intent();  intent.setclass(mainactivity.this, secondservice.class);  bindservice(intent, connection, bind_auto_create); 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -