我不知道如何在Java中实现接口

时间:2015-04-10 18:18:34

标签: java android

我在打开活动时遇到问题,我知道我必须实现OnClickListenerOnInitListener这些接口的接口。我试过@Override。在我的android工作室中,它说我必须在onclick()中的onclicklistener方法上实现摘要。我是编程的新手,所以任何帮助都非常感谢!

public void addGlossary(View v){
    Intent intent = new Intent (this, addGlossary.class);
    Button buttonZero = (Button) findViewById(R.id.buttonZero);
    startActivity(intent);

}

以下是活动的代码:

public class addGlossary extends Activity implements OnClickListener, OnInitListener {


    private int MY_DATA_CHECK_CODE = 0;


    private TextToSpeech myTTS;


    private MediaPlayer mMediaPlayer;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_glossary);


        Button speakButton = (Button)findViewById(R.id.speak);
        speakButton.setOnClickListener(this);


        Intent checkTTSIntent = new Intent();
        checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer = MediaPlayer.create(this, R.raw.button);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.start();
        mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mMediaPlayer.stop();
            }
        });

当我实现这些接口时,我尝试连接到活动,我的手机说应用程序已停止。我不知道问题是什么。任何帮助表示赞赏。

我实施的内容:onInit已经完成

@覆盖

public void onClick(View v){
Button speakButton = (Button)findViewById(R.id.speak);
speakButton.setOnClickListener(this);

}

1 个答案:

答案 0 :(得分:0)

您必须在 addGlossary 中实施所有抽象方法,将以下代码添加到类中,

1) onClick for OnClickListener

@Override
public void onClick (View v) {
    //do someting when the view clicked
}

2) onInit for OnInitListener

@Override
public void onInit (int status) {
    //do someting when tts onInit
}

Click here to learn more about Java Interface.