我在打开 Activity 时遇到问题,我知道我必须实现 OnClickListener
、OnInitListener
这些接口(interface)的接口(interface)。我尝试过使用@Override
。在我的 android studio 中,它说我必须在 onclicklistener
中实现一个关于 onclick()
方法的抽象。我是编程新手,因此非常感谢任何帮助!
public void addGlossary(View v){
Intent intent = new Intent (this, addGlossary.class);
Button buttonZero = (Button) findViewById(R.id.buttonZero);
startActivity(intent);
}
以下是该 Activity 的代码:
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();
}
});
当我实现这些接口(interface)并尝试连接到该 Activity 时,我的手机显示应用程序已停止。我不知道问题是什么。如有任何帮助,我们将不胜感激。
我实现了什么:onInit 已经完成
@覆盖
public void onClick(View v){
Button speakButton = (Button)findViewById(R.id.speak);
speakButton.setOnClickListener(this);
}
最佳答案
您必须在 addGlossary 中实现所有抽象方法,并将以下代码添加到类中,
1) onClick
for OnClickListener
@Override
public void onClick (View v) {
//do someting when the view clicked
}
@Override
public void onInit (int status) {
//do someting when tts onInit
}
关于java - 我不知道如何用Java实现接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29568211/