简体   繁体   中英

Android development, onClickListener not working

I'm completely new to Android development and I'm working through a book (practical android 4 games development) to make a game called starfighter. This is a problem on the menu screen.

The exit button used to work, and close the application down. Since I worked through playing music in the background, it no longer works. Clicking on exit stops the music, but doesn't close the application and I can't figure out why, any help please? The code is below:

SFMainMenu.java:

package com.example.starfighter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class SFMainMenu extends Activity {

    //Called when the activity is first created
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //Fire up background music
        SFEngine.musicThread = new Thread(){
            public void run(){
                Intent bgmusic = new Intent(getApplicationContext(), SFMusic.class);
                startService(bgmusic);
                SFEngine.context = getApplicationContext();
            }
        };
        SFEngine.musicThread.start();

        final SFEngine engine = new SFEngine();

        //Set menu button options
        ImageButton start = (ImageButton)findViewById(R.id.btnStart);
        ImageButton exit = (ImageButton)findViewById(R.id.btnExit);

        start.getBackground().setAlpha(SFEngine.MENU_BUTTON_ALPHA);
        start.setHapticFeedbackEnabled(SFEngine.HAPTIC_BUTTON_FEEDBACK);

        exit.getBackground().setAlpha(SFEngine.MENU_BUTTON_ALPHA);
        exit.setHapticFeedbackEnabled(SFEngine.HAPTIC_BUTTON_FEEDBACK);

        start.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                //Start the game!!!
            }
        });

        exit.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                boolean clean = false;
                clean = engine.onExit(v);
                if(clean){
                    int pid = android.os.Process.myPid();
                    android.os.Process.killProcess(pid);
                }
            }
        });
    }

}

SFEngine.java

package com.example.starfighter;

import android.view.View;
import android.content.Context;
import android.content.Intent;

public class SFEngine {

    //Constants that will be used in the game
    public static final int GAME_THREAD_DELAY = 4000;
    public static final int MENU_BUTTON_ALPHA = 0;
    public static final boolean HAPTIC_BUTTON_FEEDBACK = true;
    public static final int SPLASH_SCREEN_MUSIC = R.raw.looptrouble;
    public static final int R_VOLUME = 100;
    public static final int L_VOLUME = 100;
    public static final boolean LOOP_BACKGROUND_MUSIC = true;
    public static Context context;
    public static Thread musicThread;

    //Kill game and exit
    public boolean onExit(View v){
        try{
            Intent bgmusic = new Intent(context, SFMusic.class);
            context.stopService(bgmusic);
            musicThread.stop();
            return true;
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }
    }

}

Eclipse is giving a warning on the line musicThread.stop() - The method stop from the type Thread is deprecated.

Thanks in advance! :)

EDIT:

Also added this class, SFMusic, which may be relevant:

package com.example.starfighter;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.content.Context;

public class SFMusic extends Service {

    public static boolean isRunning = false;
    MediaPlayer player;

    @Override
    public IBinder onBind(Intent arg0){
        return null;
    }

    @Override
    public void onCreate(){
        super.onCreate();
        setMusicOptions(this, SFEngine.LOOP_BACKGROUND_MUSIC, SFEngine.R_VOLUME, SFEngine.L_VOLUME,
                SFEngine.SPLASH_SCREEN_MUSIC);
    }

    public void setMusicOptions(Context context, boolean isLooped, int rVolume, int lVolume, int soundFile){
        player = MediaPlayer.create(context, soundFile);
        player.setLooping(isLooped);
        player.setVolume(rVolume, lVolume);
    }

    public int onStartCommand(Intent intent, int flags, int startId){
        try{
            player.start();
            isRunning = true;
        } catch(Exception e){
            isRunning = false;
            player.stop();
        }
        return 1;
    }

    public void onStart(Intent intent, int startId){

    }

    public void onStop(){
        isRunning = false;
    }

    public IBinder onUnBind(Intent arg0){
        //TODO Auto-generated method stub
        return null;
    }

    public void onPause(){

    }

    @Override
    public void onDestroy(){
        player.stop();
        player.release();
    }

    @Override
    public void onLowMemory(){
        player.stop();
    }

}

I tried adding a println as the first line of code in the onClick method. Nothing was output to the console so I'm not sure the method is being called. But then I'm confused as to why the music is still stopping.

Use 

System.exit(0);

Before that 

stopservice();
Thread.stop();

should be done

I suggest you to remove code that kills your process by ID, because it's a bad practice, and Android might restart killed app. Use finish(); instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM