Reputation: 27
I have some problems with android programming. I am trying to open an activity on my phone, but it fails. It says unfortunately the program has stopped. I do not know how to solve this, I try to connect from this method below. Any help is appreciated. Please ask if I did not specify enough.
public void addGlossary(View v){
Intent intent = new Intent (this, addGlossary.class);
Button buttonZero = (Button) findViewById(R.id.buttonZero);
startActivity(intent);
}
Here is the code for the activity:
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);
}
}
Upvotes: 0
Views: 318
Reputation: 7494
Your adGlossary class is an abstract class. Android needs to see a concrete Activity implementation to start one.
Remove the abstract keyword and assuming you have the activity in your manifest, it should start.
**EDIT:**As Chris Stratton noted in the comments, the likely reason the abstract was placed was to go around implementing the OnClickListener and the OnInitListener. While the code may have compiled, the solution is still unworkable.
So apart from removing the abstract qualifier, you will also have to implement the interfaces.
Solution: Remove the abstract modifier. The compiler should immediately detect the unimplemented methods and should ask you for the option to add unimplemented methods or mark the class as abstract (the red squiggly lines will show up and you should get these options when you move your mouse over it). Ask the compiler to add unimplemented methods. You should now have:
@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
Just implement your logic within the above methods and that should do it.
Upvotes: 3