rest - Android @RestService injection via androidannotations results into nullPointerException -
i gettin null pointer exception on line wsresult = restclientinterface.skuska(); restclientinterface. here sample:
import org.springframework.http.converter.json.gsonhttpmessageconverter; import com.googlecode.androidannotations.annotations.rest.get; import com.googlecode.androidannotations.annotations.rest.rest; @rest(rooturl = "http://my_ip_address/webresources", converters = {gsonhttpmessageconverter.class}) public interface restclient { @get("/persons") string skuska(); }
and using in fragment
@efragment public class homefragment extends fragment { private button ws; private textview wsstring; private string wsresult; @restservice restclient restclientinterface; wsstring = (textview) view.findviewbyid(r.id.wsstring); ws = (button) view.findviewbyid(r.id.ws); ws.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { getwsresult(); wsstring.settext(wsresult); } }); homecoming view; } @background public void getwsresult() { wsresult = restclientinterface.skuska(); }
your restclient should injected correctly aa after fragment ready. copy/paste generated class see what's happening ?
moreover, you're calling getwsresult()
(which background method) , after you're setting result of rest phone call in textview. can't if result arrived before you're trying setting in object.
you should seek code 1 :
@efragment(r.layout.homefragment) public class homefragment extends fragment { @viewbyid button ws; @viewbyid textview wsstring; @restservice restclient restclientinterface; private string wsresult; @click void wsstringclicked() { getwsresult(); } @background void getwsresult() { wsresult = restclientinterface.skuska(); } @uithread void updateui() { wsstring.settext(wsresult); } }
edit: removed private
on @viewbyid
annotated fields
edit2: thinking it. how using fragment in activities ? using either @fragmentbyid
or @fragmentbytag
?
android rest service null android-annotations
No comments:
Post a Comment