user3107025
user3107025

Reputation: 33

Eclipse OnClickListener MainActivity errors

I got a problem with my program.

Here's the error:

The type MainActivity must implement the inherited abstract method View.OnClickListener.onClick(View)

Here's the code:

import android.os.Bundle;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnInitListener, OnClickListener {

    private TextToSpeech tts;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tts = new TextToSpeech(this, this);
    }

    public void OnClick (View view){
        EditText et = (EditText) findViewById (R.id.editText1);
        tts.speak(et.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
    }

    public void onInit (int status){
        Button button = (Button) findViewById (R.id.button1);
        button.setOnClickListener(this);
        tts.setLanguage(Locale.GERMAN);
        tts.setSpeechRate(1);
    }
}

I just started programming with eclipse and I'm really a beginner.

So what do I have to do to solve the problem?

Upvotes: 0

Views: 2035

Answers (4)

Alexis C.
Alexis C.

Reputation: 93872

OnClickListener is an interface. You have to implement all the methods provided by this interface.

I think this is what you wanna do here :

public void OnClick (View view){
    EditText et = (EditText) findViewById (R.id.editText1);
    tts.speak(et.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}

But this is not the right signature, it should be :

@Override
public void onClick (View view){ //note the 'o'
    EditText et = (EditText) findViewById (R.id.editText1);
    tts.speak(et.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}

Note that adding the @Override annotation is not required but it helps to prevent errors. If a method marked with @Override fails to correctly override a method in one of its superclasses, the compiler generates an error.

Upvotes: 1

Kevin Bowersox
Kevin Bowersox

Reputation: 94499

You almost had it! Just change the name of the method to onClick so that it implements theonclick method as specified by the OnClickListener interface.

When a class implements an interface it must provide an implementation for each method listed on the interface. The classes implementation must match the signature (name, arguments, return type) specified on the interface.

public void onClick (View view){
    EditText et = (EditText) findViewById (R.id.editText1);
    tts.speak(et.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}

Upvotes: 1

Vaibhav Agarwal
Vaibhav Agarwal

Reputation: 4499

Please clean your project and build it again

You have to implement all the methods provided by OnClickListener interface. when you implement onClickListener in your activity your onclick method should be overided like

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    EditText et = (EditText) findViewById (R.id.editText1);
    tts.speak(et.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}

Upvotes: 1

Raghunandan
Raghunandan

Reputation: 133560

The type MainActivity must implement the inherited abstract method View.OnClickListener.onClick(View)

You need to implement onClick(View). Since it was not implemented you get the error.

So Change to

@Override
public void onClick (View view){ //OnClick  You has O in caps
EditText et = (EditText) findViewById (R.id.editText1);
tts.speak(et.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}

Add Override annotation and change OnClick to onClick.

Upvotes: 2

Related Questions