I'm trying to bind a service in my main activity and am getting a null pointer exception. In order to bind the service, i've added onStart() and onStop() methods to the activity, which in themselves seem to be causing the null pointer exception. That's the conclusion i've come to through careful commenting out of lines of code.
Just to be clear, and also I suppose to provide further detail:
Does not Cause Null Pointer Exception:
public class MainActivity extends Activity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
public VivaClientService VivaClient;
private static final String TAG = "Viva_MainActivity";
private Intent ClientIntent;
private ServiceConnection ClientConnect = new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "service connected");
LocalBinder binder = (LocalBinder)service;
VivaClient = binder.getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "service disconnected");
}
};
//Fragment managing the behaviors, interactions and presentation of the navigation drawer.
private NavigationDrawerFragment mNavigationDrawerFragment;
//Used to store the last screen title. For use in {@link #restoreActionBar()}.
private CharSequence mTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
ClientIntent = new Intent(this, VivaClientService.class);
}
/*
@Override
protected void onStart(){
//this.bindService(ClientIntent, ClientConnect, Context.BIND_AUTO_CREATE);
//this.startService(ClientIntent);
//JSONObject about = VivaClient.getAbout();
//System.out.println(about.toString());
}
@Override
protected void onStop(){
// this.stopService(ClientIntent);
// this.unbindService(ClientConnect);
}
*/
Causes Null Pointer Exception:
public class MainActivity extends Activity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
public VivaClientService VivaClient;
private static final String TAG = "Viva_MainActivity";
private Intent ClientIntent;
private ServiceConnection ClientConnect = new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "service connected");
LocalBinder binder = (LocalBinder)service;
VivaClient = binder.getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "service disconnected");
}
};
//Fragment managing the behaviors, interactions and presentation of the navigation drawer.
private NavigationDrawerFragment mNavigationDrawerFragment;
//Used to store the last screen title. For use in {@link #restoreActionBar()}.
private CharSequence mTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
ClientIntent = new Intent(this, VivaClientService.class);
}
@Override
protected void onStart(){
//this.bindService(ClientIntent, ClientConnect, Context.BIND_AUTO_CREATE);
//this.startService(ClientIntent);
//JSONObject about = VivaClient.getAbout();
//System.out.println(about.toString());
}
@Override
protected void onStop(){
// this.stopService(ClientIntent);
// this.unbindService(ClientConnect);
}
Literally, the only difference between the two is the inclusion of the onStart and onStop methods which, when included, are blank. I don't understand why that should cause an error. Plz help ;_; thanks!