Я пытаюсь связать службу в своей основной деятельности, и я получаю исключение нулевого указателя. Чтобы связать службу, я добавил в действие методы onStart() и onStop(), которые сами по себе, как представляется, вызывают исключение нулевого указателя. Это вывод, который я пришел через тщательный комментарий из строк кода.Включение метода onStart вызывает исключение Null Pointer
Просто чтобы быть ясно, и я также полагаю, чтобы обеспечить дополнительную деталь:
не вызывает 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);
}
*/
Причины 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);
}
Буквально, единственная разница между ними - это включение методов onStart и onStop, которые при включении являются пустыми. Я не понимаю, почему это должно вызывать ошибку. Помощь Plz; _; благодаря!
Было бы полезно, если бы вы отправили сообщение об ошибке из LogCat, но обычно, если вы намерены переопределить метод, вы также должны вызвать метод суперклассов. Подобно тому, что вы сделали с помощью команды super.onCreate (savedInstanceState); –