[android-developers] Help needed for dealing with NullPointerException when calling functions on a service just after binding it
I am binding to a service(which I have created by extending Service class) and just after it, I am calling function(s) provided by service interface. [Code Snippet at end for reference] But only after 15~20ms when the Service has been bound asynchronously, I am able to use the service interface to call service functions. Before that time, the instance of the service interface is null and I am unable to call upon the methods exposed by the Service. Is there some way by which I can make sure that the service binding is complete before any access is made to functions provided by the service ? Please help Thanks in advance. -- Code Snippet of an Activity class for Reference -- ... private ISomeServiceInterface myService .. private ServiceConnection progressBarServiceConn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { myService = ISomeServiceInterface.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { myService = null; } }; public void onCreate(Bundle savedInstanceState) { //create intent to connect to service Intent serviceIntent = new Intent("some.uri"); bindService(serviceIntent, progressBarServiceConn, Context.BIND_AUTO_CREATE); //access the service Integer someValue = myService.someFunc(1, 2); // NullPointerException here - How to make sure service should be running before any function is called on the service ? } -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Help needed for dealing with NullPointerException when calling functions on a service just after binding it
Thanks Mark for replying. The issue is that I need to call the service methods as soon as it is connected. So, Is there any way by which I can execute the functions of service JUST AFTER binding is complete ? It takes around 10-20ms for binding, I tried to make use Thread.sleep(30); but it delayed the service from being connected by the same sleep duration, therefore not solving the problem. On Jul 20, 6:33 pm, Mark Murphy wrote: > On Tue, Jul 19, 2011 at 12:21 PM, animeshsi wrote: > > Is there some way by which I can make sure that the service binding is > > complete before any access is made to functions provided by the > > service ? > > Don't try using the Binder until onServiceConnected() is called. > > -- > Mark Murphy (a Commons > Guy)http://commonsware.com|http://github.com/commonsguyhttp://commonsware.com/blog|http://twitter.com/commonsguy > > Android Training...At Your Office:http://commonsware.com/training -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en