Skip to content Skip to sidebar Skip to footer

Android: Trouble With BindService() -> Service Is Null

I'm having a problem with binding service to an activity. I get playing_service==null. I can't find what I'm doing wrong. Why is playing_service null?? MyActivity class: private pl

Solution 1:

Your service might not be null because binding a service is an asynchronous method, so instead of checking the availability of your service yet after calling the bind method, you should do it in your service connection implementation, for e.g.:

private ServiceConnection service_conn=new ServiceConnection(){
    public void onServiceConnected(ComponentName className, IBinder service) {
        LocalBinder binder=(LocalBinder)service;
        playing_service=binder.getService();

        if(playing_service != null){
            Log.i("service-bind", "Service is bonded successfully!");

            //do whatever you want to do after successful binding
        }
    }
    public void onServiceDisconnected(ComponentName arg0) {
        // TODO Auto-generated method stub

    }
};

Post a Comment for "Android: Trouble With BindService() -> Service Is Null"