I'm attempting to bind a service from a fragment the same way I have done succesfully in an activity, but when I try to call a method on the service I get a NullPointerException - Obviously because the service is null. Now is there some problem with binding to the service in onStart or am I simply doing it wrong?
@Override
public void onStart() {
super.onStart();
Intent intent = new Intent(getActivity(), LiteTrickService.class);
getActivity().registerReceiver(receiver, new IntentFilter(LiteTrickService.BROADCAST_ACTION));
getActivity().registerReceiver(receiver, new IntentFilter(LiteTrickService.BROADCAST_FAIL));
getActivity().bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
@Override
public void onStop() {
super.onStop();
getActivity().unbindService(mConnection);
getActivity().unregisterReceiver(receiver);
mBound = false;
}
edit: Sorry. That's my mistake for not giving this question enough thought. mConnection is a ServiceConnection and looks like this :
private ServiceConnection mConnection = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
Stacktrace :
01-03 15:21:22.355: E/AndroidRuntime(12360): FATAL EXCEPTION: main
01-03 15:21:22.355: E/AndroidRuntime(12360): java.lang.NullPointerException
01-03 15:21:22.355: E/AndroidRuntime(12360): at lite.hattrick.players.PlayerRankingFragment.onOptionsItemSelected(PlayerRankingFragment.java:205)
And this is would be the exact place where the exception is thrown : case POPULATE_ID:
if (hasData) {
return false;
}
if(!mBound)
getActivity().bindService(new Intent(getActivity().getApplicationContext(), LiteTrickService.class), mConnection, Context.BIND_AUTO_CREATE);
mService.refreshPlayers(); // Null Pointer Exception as mService is null
pBar.setVisibility(View.VISIBLE);
return true;
To provide binding for a service, you must implement the onBind() callback method. This method returns an IBinder object that defines the programming interface that clients can use to interact with the service.
Started services run until they are stopped or destroyed and do not inherently provide a mechanism for interaction or data exchange with other components. Bound services, on the other hand, provide a communication interface to other client components and generally run until the last client unbinds from the service.
A service is bound when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC).
I solved the problem. Turns out I had forgotten to provide service declaration in my manifest with its correct package name.
by changing
<service android:name=".LiteTrickService" />
to
<service android:name="lite.hattrick.services.LiteTrickService" />
I solved the problem and the service is now connecting as expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With