我在android编程方面遇到了一些问题。我正在尝试在手机上打开一个活动,但它失败了。它说不幸的是该计划已经停止。我不知道如何解决这个问题,我尝试从下面这个方法连接。任何帮助表示赞赏。请问我是否指定不够。
public void addGlossary(View v){
Intent intent = new Intent (this, addGlossary.class);
Button buttonZero = (Button) findViewById(R.id.buttonZero);
startActivity(intent);
}
以下是活动的代码:
public abstract 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();
}
});
}
public void buttonReturn(View v){
Intent intent = new Intent (this, MainActivity.class);
Button buttonreturn = (Button) findViewById(R.id.buttonReturn);
startActivity(intent);
}
public void startGame(View v){
Intent intent = new Intent (this, firstLevel.class);
Button buttonstart = (Button) findViewById(R.id.buttonStart);
startActivity(intent);
}
public void onInit(int initStatus) {
if(myTTS.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE) {
myTTS.setLanguage(Locale.US);
}
else if (initStatus == TextToSpeech.ERROR) {
Toast.makeText(this, "Sorry! Text To Speech failed...", Toast.LENGTH_LONG).show();
}
}
private void speakWords(String speech){
myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
}
public void buttonAdd(){
//lagrar texten från textfältet man knappade in till string
EditText enteredText = (EditText)findViewById(R.id.enter);
String words = enteredText.getText().toString();
speakWords(words);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
myTTS = new TextToSpeech(this, this);
}
else {
Intent installTTSIntent = new Intent();
installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installTTSIntent);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_add_glossary, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:3)
您的adGlossary类是一个抽象类。 Android需要查看具体的Activity实现来启动它。
删除abstract关键字并假设您在清单中有活动,它应该开始。
**编辑:**正如Chris Stratton在评论中指出的那样,摘要的可能原因是要实现OnClickListener和OnInitListener。虽然代码可能已编译,但解决方案仍然不可行。
除了删除抽象限定符之外,还必须实现接口。
解决方案:删除抽象修饰符。编译器应该立即检测未实现的方法,并且应该要求您选择添加未实现的方法或将类标记为抽象(红色波浪线将显示,当您将鼠标移到它上面时应该获得这些选项)。请求编译器添加未实现的方法。你现在应该:
@Override
public void onClick(View view) {
// Do something when clicked
// Implement logic here
}
@Override
public void onInit(int status) {
// Implement logic here
}
// It will be all done and everything should work
只需在上述方法中实现您的逻辑即可。