3

I referred to the following questions already but could not find an answer:

Here is my code:

@Override
    public void onStart() {
        super.onStart();
        Context context = getApplicationContext();
        Intent intent = new Intent(context, PodService.class);
        context.bindService(intent, mPodServiceConn, Context.BIND_AUTO_CREATE);

    }




    private ServiceConnection mPodServiceConn = new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName className, IBinder service) {
                    Log.e(TAG, "Pod: Service Connected");
                  mPodService = IPodService.Stub.asInterface(service); //here i am getting NullPointerException
                }
        }

and My service class contains nothing, only this much, i have shown it bellow

public class PodService extends Service {
@Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        Log.d("bound", "bound");
        return null;
    }

 static class PodSeviceStub extends IPodService.Stub {

//here i implemented unimplemented methods

    }
}

But in lolcat i am getting only "bound" message from onBind() function, but not printing "Pod: Service Connected", means service is started successfully. and in lolcat i am getting NullPointerException and also mentioned in manifest file too.

TT--
  • 2,956
  • 1
  • 27
  • 46
Charan Pai
  • 2,288
  • 5
  • 32
  • 44

1 Answers1

2

I rewrote the service class so that onBind() returns IBinder as follwos.

public class PodService extends Service {
@Override
    public IBinder onBind(Intent intent) {

        Log.d("bound", "bound");
        return mBinder; // returns IBinder object
    }

    private final IBinder mBinder = new PodSeviceStub(this);

 static class PodSeviceStub extends IPodService.Stub {

         WeakReference<PodService> mService;

        public PodSeviceStub(PodService service) {// added a constructor for Stub here
            mService = new WeakReference<PodService>(service);
        }
//here i implemented unimplemented methods

    }
}

and now it works.

Charan Pai
  • 2,288
  • 5
  • 32
  • 44