java - Using interface callbacks in Android -
i trying write simple chat application on android.
it takes speech, converts text, sends server.
i want able use texttospeech read response server gives.
i keep getting java.lang.nullpointerexception: attempt invoke interface method 'void {myappname}.mycallback.callbackcall()' on null object reference
. have looked @ other questions similar answers, conflicting, "the interface should instantiated first" or "you don't instantiate interfaces", etc., , confusing me.
the receiver:
public class mainactivity extends actionbaractivity implements texttospeech.oninitlistener, mycallback { ... private void sendtoserver(string msg) { cthread = new clientthread(); cthread.msg = msg; thread thread = new thread(cthread); thread.start(); } @override public void callbackcall() { log.d("callback", cthread.serverresponsesaved); // speaking here. } }
the actual code responsible messages:
interface mycallback { public void callbackcall(); } public class clientthread implements runnable { string address = "xxx.xxx.x.xx"; int port = yyyyy; boolean connected = false; string msg = ""; private string serverresponse; string serverresponsesaved; mycallback callback; @override public void run() { try { socket socket = new socket(address, port); this.connected = true; while(connected) { try { printwriter out = new printwriter(new bufferedwriter(new outputstreamwriter(socket.getoutputstream())), true); out.println(this.msg); bufferedreader in = new bufferedreader(new inputstreamreader(socket.getinputstream())); if ((serverresponse = in.readline()) != null) { serverresponsesaved = serverresponse; log.i("server says", serverresponsesaved); callback.callbackcall(); } } catch (exception e) { e.printstacktrace(); } } } catch (ioexception e) { e.printstacktrace(); this.connected = false; } } }
is way i'm implementing interface wrong?
edit:
trying instantiate by: callback = new mycallback();
gives me 'mycallback abstract. cannot instantiated.'
error.
the use of interface based on: here
you not setting callback
anywhere. define mycallback
, give name callback
still has nothing assigned it, hence nullpointerexception
when hit callback.callbackcall();
i'm assuming want set mainactivity
linked, have pass clientthread
through constructor like:
public class clientthread implements runnable { public clientthread(mycallback callback){ this.callback = callback; } }
then change:
cthread = new clientthread();
to
cthread = new clientthread(this);
however if callback function wants ui related things, or should run on main thread, have use handler post main thread. if want that, easiest pass in context
clientthread
, this:
public class clientthread implements runnable { /*your things*/ context mctx; public clientthread(context context){ this.mctx = context; if(context instanceof mainactivity){ this.callback = (mainactivity) callback; } } }
then change callback bit to:
handler mainhandler = new handler(mctx.getmainlooper()); mainhandler.post(new runnable() { @override public void run() { callback.callbackcall(); } });
Comments
Post a Comment