Reputation: 1225
There is a service and activity.
Service class has one method
methodA().
Now I have binded the service in Activity and got the instance of service through service connection.
problem is I can't access the instance of service inside onResume() method
Public void onResume(){
forExample. mService.methodA // is throwing null pointer exception
}
update: This is how I created a service instance
class A extends Activity{
public ServiceClass mService = null; // service objecct
void onCreate(){
Intent ServiceIntent = new Intent(this,BleWrapper.class);
bindService(ServiceIntent,mServiceConnection,BIND_AUTO_CREATE);
mLeService = BLEService.getInstance();
}
Public void onResume(){
mService.methodA // is throwing null pointer exception
}
}
my Service connection is
public final ServiceConnection mServiceConnection= new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
mService = ((BleWrapper.LocalBinder) service).getService();
)
Can any one please help me.
Upvotes: 0
Views: 1052
Reputation: 7901
Of course it would throw NullPointerException
because you have not initialized a service at all.
Initialize a service in onCreate()
method, And then you can use mService.methodA()
on onResume()
class A extends Activity{
public ServiceClass mService = null; // service objecct
public void onCreate(){
mService = new ServiceClass();
}
public void onResume(){
mService.methodA();
}
}
Note : Whenever you create any object, it must be initialized before getting used.
Upvotes: 1
Reputation: 638
You can get the instance of that service somewhere else in your Activity. The result can be given to the onResume()
method.
Upvotes: 1