我正在與libGDX一個小的Android遊戲。 的結構如下:libgdx停止與類的運行音樂
主>屏幕:濺射屏幕>屏幕:的MainMenu>屏幕:設置或GameScreen
我對音樂的狀態的偏好(如果運行「音樂」爲true)。 如果音樂爲真,則音樂將在MainMenu中開始播放。 現在用戶應該能夠在設置屏幕中打開和關閉音樂。 要管理音樂,我創建了一個名爲MusicPlayer的類。 守則:
的MainMenu:
public class StartScreen implements Screen {
MusicPlayer musicPl;
Preferences scorepref;
boolean music;
(...)
@Override
public void show() {
(...)
scorepref = Gdx.app.getPreferences("Highscore");
musicPl = new MusicPlayer();
music = scorepref.getBoolean("music");
if(music){
musicPl.play();
}
}
@Override
public void dispose() {
musicPl.dispose();
}
}
設置:
public class SettingScreen implements Screen {
Preferences scorepref;
//Setting Values
boolean tut = false;
boolean music = false;
boolean sounds = false;
int theme = 0;
//
MusicPlayer musicPl;
int touchX = 0;
int touchY = 0;
boolean touchD = false;
boolean touchU = false;
@Override
public void show() {
(handling Input and setting touchX,touchY,touchD and touchU)
(...)
musicPl = new MusicPlayer();
scorepref = Gdx.app.getPreferences("Highscore");
}
@Override
public void render(float delta) {
tut = scorepref.getBoolean("tut");
music = scorepref.getBoolean("music");
sounds = scorepref.getBoolean("sounds");
(...)
//if Touched
if(touchD == true && touchU == true){
touchD = false;
touchU = false;
//wo?
Vector3 posn = new Vector3(Gdx.input.getX(),Gdx.input.getY(), 0);
camera.unproject(posn);
if(){
(...)
} else if (posn.x >= -192 && posn.x <= 192 && posn.y <= 53 && posn.y >= -75) {
if(music){
music = false;
scorepref.putBoolean("music", music);
scorepref.flush();
musicPl.stop(); //Error line 206 is here
} else {
music = true;
scorepref.putBoolean("music", music);
scorepref.flush();
musicPl.play();
}
} (...)
} else if (posn.x >= -344 && posn.x <= -248 && posn.y <= 543 && posn.y >= 463) {
((Game) Gdx.app.getApplicationListener()).setScreen(new StartScreen());
}
//var speichern
scorepref.putBoolean("tut", tut);
scorepref.putBoolean("music", music);
scorepref.putBoolean("sounds", sounds);
scorepref.flush();
}
@Override
public void dispose() {
musicPl.dispose();
}
}
MusicPlayer:
public class MusicPlayer{
Music menuMusic;
boolean isrunning;
Preferences scorepref;
public void play(){
scorepref = Gdx.app.getPreferences("Highscore");
if(scorepref.getBoolean("running") == true){
} else {
menuMusic = Gdx.audio.newMusic(Gdx.files.internal("Theme.mp3"));
menuMusic.setLooping(true);
scorepref.putBoolean("running", true);
scorepref.flush();
menuMusic.play();
}
}
public void stop(){
scorepref = Gdx.app.getPreferences("Highscore");
scorepref.putBoolean("running", false);
scorepref.flush();
menuMusic.stop(); //Error line 33 is here
}
public void dispose(){
menuMusic.dispose();
}
}
我的問題: 當我在設置屏幕的tunring上關閉工作正常。 當我打開音樂並返回到MainScreen時,音樂仍在播放。到現在爲止還挺好。 但是,當我返回SettingsScreen並運行音樂時,我想關閉應用程序崩潰。
我認爲崩潰是由停止方法引起的,因爲MusicPlayer不知道該停止什麼。但是,我怎麼能告訴他,或者我怎樣才能用不同的技術來解決這個問題呢?
感謝您的幫助。
P.S. 這裏是我的錯誤,當我在桌面上運行應用程序:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at de.hatgames.canone.MusicPlayer.stop(MusicPlayer.java:33)
at de.hatgames.canone.SettingScreen.render(SettingScreen.java:206)
at com.badlogic.gdx.Game.render(Game.java:46)
at de.hatgames.canone.CanoneMain.render(CanoneMain.java:26)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
CanoneMain.java:26僅僅是這樣的:
super.render();
請添加您收到的錯誤日誌 – tdgtyugdyugdrugdr