我试图在我的主要活动绑定一个服务,我得到一个空指针异常。为了绑定服务,我在活动中添加了onStart()和onStop()方法,这些方法本身似乎会导致空指针异常。这是我通过仔细注释代码行得出的结论。
只是为了清楚,我也想提供更多的细节:
不会导致空指针异常:
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);
}
*/
导致空指针异常:
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);
}
从字面上看,两者之间的唯一区别是包含onStart和onStop方法,当包含时,它们是空白的。我不明白为什么会产生错误。请帮忙;_;谢谢! 您没有从覆盖中调用super.onStart()
或super.onStop()
,这将导致活动抛出异常。