コピーしようとしました: [http://stackoverflow.com/questions/11490236/android-comprehensive-failproof-music-service-across-multiple-activites][1] ただし、次の場所で null ポインター例外が発生します: StartupActivity .getService().musicStart(); これが null になる理由はありますか? (要約された)メインJavaの私のバージョンは次のとおりです。
public class homescreenfruit extends Activity {
// bounded service
private static MusicService mBoundService;
// whetere service is bounded or not
private boolean mIsBound;
SharedPreferences myPrefs;
SharedPreferences.Editor prefsEditor;
boolean snd1 = true;
boolean snd2 = true;
boolean snd3 = true;
Handler myHandler = new Handler();
// int lastsongplayed=1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homescreen);
doBindService();
myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
final SharedPreferences.Editor prefsEditor = myPrefs.edit();
SoundManager.getInstance();
SoundManager.initSounds(this);
SoundManager.loadSounds();
{ // audio settings setup and run
snd3 = myPrefs.getBoolean("snd3return", true);
// ********************FIALS BELOW*********************
if (snd3==true){
homescreenfruit.getService().musicStart(); // FAILS HERE WITH NPE
}
ImageButton ibs3 = (ImageButton) findViewById(R.id.sound3); // this is the music
ibs3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.e("sound3 start : ", " yep");
ImageButton ibs3 = (ImageButton) findViewById(R.id.sound3);
if (snd3==true){
homescreenfruit.getService().musicStop();
prefsEditor.putBoolean("snd3return", false); snd3=false;
ibs3.setBackgroundResource(R.drawable.noteoff);
}
else if (snd3==false){
prefsEditor.putBoolean("snd3return", true); snd3=true;
ibs3.setBackgroundResource(R.drawable.noteon);
homescreenfruit.getService().musicStart();
// restart playing the music - add programming
homescreenfruit.getService().musicStart();
}
prefsEditor.commit();
}
});
final Runnable playmusic = new Runnable() //
{
public void run() {
if (snd3==true){
homescreenfruit.getService().musicStart();}
}
};
public void onBackPressed() {
if (snd3=true)
SoundManager.playSound(96, 1);
Log.e("onBack pressed", "onBack pressed");
finish();
}
public void onPause() {
super.onPause();
if (snd3=true)
{
homescreenfruit.getService().musicPause();
}
Log.e("onPause", "onPause");
}
public void onStop() {
super.onStop();
if (snd3=true){
homescreenfruit.getService().musicStop();
}
}
public void onResume() {
super.onResume();
if (snd3==true){
homescreenfruit.getService().musicStart();
}
}
public void onDestroy() {
super.onDestroy();
doUnbindService();
}
private final ServiceConnection mServiceConnection = new ServiceConnection() {
// removed override
public void onServiceConnected(ComponentName className, IBinder service) {
setService(((MusicService.LocalBinder) service).getService());
}
// removed override
public void onServiceDisconnected(ComponentName className) {
setService(null);
}
};
private void doBindService() {
Intent service = new Intent(getBaseContext(), MusicService.class);
// start service and bound it
startService(service);
bindService(new Intent(this, MusicService.class), mServiceConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
}
private void doUnbindService() {
if (mIsBound) {
// Detach existing connection.
unbindService(mServiceConnection);
mIsBound = false;
}
}
public static MusicService getService() {
return mBoundService;
}
private static void setService(MusicService mBoundService) {
homescreenfruit.mBoundService = mBoundService;
}
}
CarefulMediaPlayer と Music Service の両方を、リンクごとに実装しました。あれは正しいですか?