goedkoop
goedkoop

Reputation: 119

The type new View.OnClickListener must implement the inherited abstract method

So I just started programming, but I dont really get the method OnClickListener. I already google the error I have: "The type new View.OnClickListener(){} must implement the inherited abstract method View.OnClickListener.OnClick(View)" and I tried to change some things in my code, but it still doens't work. Can anyone tell me exactly what I need to change in my code? Thanks in advance.

MainMenuScreen.java:

package com.wouter.testjk;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

public class MainMenuScreen extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main_menu);
 //error on the line below
        ((Button) findViewById(R.id.one_player)).setOnClickListener(new OnClickListener() { //the error is here
            public void onClick(View V) {
                Log.d("DEBUG", "One Player Button Pressed!");
                Intent intent = new Intent(MainMenuScreen.this, TicTacToeGame.class);
                intent.putExtra("gameType", true);
                startActivityForResult(intent, 0);
            }
        });
        ((Button) findViewById(R.id.two_player)).setOnClickListener(new OnClickListener() {
            public void onClick(View V) {
                Log.d("DEBUG", "Two Player Button Pressed!");
                Intent intent = new Intent(MainMenuScreen.this, TicTacToeGame.class);
                intent.putExtra("gameType", false);
                startActivityForResult(intent, 0);
            }
        });

        ((Button) findViewById(R.id.exit_game)).setOnClickListener(new OnClickListener() {
            public void onClick(View V) {
                Log.d("DEBUG", "Exit Game Button Pressed!");
                MainMenuScreen.this.finish();
            }
        });
    }
}

MainActivity.java:

package com.wouter.testjk;


import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.wouter.testjk.R;


public class MainActivity extends Activity implements OnClickListener{

    private TicTacToeGame mGame;

    private Button mBoardButtons[];

    private TextView mInfoTextView;
    private TextView mPlayeroneCount;
    private TextView mTieCount;
    private TextView mPlayertwoCount;
    private TextView mPlayeroneText; 
    private TextView mPlayertwoText;

    private int mPlayeroneCounter = 0;
    private int mTieCounter = 0;
    private int mPlayertwoCounter = 0;

    private boolean mPlayeroneFirst = true;
    private boolean mIsSinglePlayer = false;
    private boolean mIsPlayerOneTurn = true;
    private boolean mGameOver = false;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        boolean mGameType = getIntent().getExtras().getBoolean("gametype");

        mBoardButtons = new Button[mGame.getBOARD_SIZE()];
        mBoardButtons[0] = (Button) findViewById(R.id.one);
        mBoardButtons[1] = (Button) findViewById(R.id.two);
        mBoardButtons[2] = (Button) findViewById(R.id.three);
        mBoardButtons[3] = (Button) findViewById(R.id.four);
        mBoardButtons[4] = (Button) findViewById(R.id.five);
        mBoardButtons[5] = (Button) findViewById(R.id.six);
        mBoardButtons[6] = (Button) findViewById(R.id.seven);
        mBoardButtons[7] = (Button) findViewById(R.id.eight);
        mBoardButtons[8] = (Button) findViewById(R.id.nine);

        Button mTen = (Button) findViewById(R.id.ten);
        mTen.setOnClickListener(this);
        Button mEleven = (Button) findViewById(R.id.eleven);
        mEleven.setOnClickListener(this);



        mInfoTextView = (TextView) findViewById(R.id.information);
        mPlayeroneCount = (TextView) findViewById(R.id.humancount);
        mTieCount = (TextView) findViewById(R.id.tiesCount);
        mPlayertwoCount = (TextView) findViewById(R.id.androidCount);
        mPlayeroneText = (TextView) findViewById(R.id.human);
        mPlayertwoText = (TextView) findViewById(R.id.android);

        mPlayeroneCount.setText(Integer.toString(mPlayeroneCounter));
        mTieCount.setText(Integer.toString(mTieCounter));
        mPlayeroneCount.setText(Integer.toString(mPlayertwoCounter));

        mGame = new TicTacToeGame();

        startNewGame(mGameType);

    }

    private void startNewGame(boolean isSingle)
    {
        //some code here
    }

    private class ButtonClickListener implements View.OnClickListener
    {
        int location;

        public ButtonClickListener(int location)
        {
            this.location = location;
        }

        @Override
        public void onClick(View view) {
            if (!mGameOver)
            {
                if(mBoardButtons[location].isEnabled())
                {
                    if(mIsSinglePlayer)
                    {
                         setMove(mGame.PLAYER_ONE, location);

                         int winner = mGame.checkForWinner();

                         if (winner == 0)
                         {
                             mInfoTextView.setText(R.string.turn_computer);
                             int move = mGame.getComputerMove();
                             setMove(mGame.PLAYER_TWO, move);
                             winner = mGame.checkForWinner();

                         }
                         if (winner == 0)
                                 mInfoTextView.setText(R.string.turn_human);
                         else if (winner == 1)
                         {
                             mInfoTextView.setText(R.string.result_tie);
                             mTieCounter++;
                             mTieCount.setText(Integer.toString(mTieCounter));
                             mGameOver = true;
                         }       
                         else if (winner ==2)
                         {
                             mInfoTextView.setText(R.string.result_human_wins);
                             mPlayeroneCounter++;
                             mPlayeroneCount.setText(Integer.toString(mPlayeroneCounter));
                             mGameOver = true;
                         }
                         else if (winner ==3)
                         {
                             mInfoTextView.setText(R.string.result_android_wins);
                             mPlayertwoCounter++;
                             mPlayertwoCount.setText(Integer.toString(mPlayertwoCounter));
                             mGameOver = true;
                         }

                    }
                    else
                    {
                        if(mIsPlayerOneTurn)
                        {
                            setMove(mGame.PLAYER_ONE, location);
                        }
                        else
                        {setMove(mGame.PLAYER_TWO, location);

                        }


                        int winner = mGame.checkForWinner();

                        if (winner == 0)
                        {
                            if(mIsPlayerOneTurn)
                            {
                                mInfoTextView.setText(R.string.turn_player_two);
                                mIsPlayerOneTurn = false;
                            }
                            else
                            {
                                mInfoTextView.setText(R.string.turn_player_one);
                                mIsPlayerOneTurn = true;
                            }
                        }
                        else if (winner == 1)
                        {
                            mInfoTextView.setText(R.string.result_tie);
                            mTieCounter++;
                            mTieCount.setText(Integer.toString(mTieCounter));
                            mGameOver = true;
                        }       
                        else if (winner ==2)
                        {
                            mInfoTextView.setText(R.string.player_one_wins);
                            mPlayeroneCounter++;
                            mPlayeroneCount.setText(Integer.toString(mPlayeroneCounter));
                            mGameOver = true;
                        }
                        else if (winner ==3)
                        {
                            mInfoTextView.setText(R.string.player_two_wins);
                            mPlayertwoCounter++;
                            mPlayertwoCount.setText(Integer.toString(mPlayertwoCounter));
                            mGameOver = true;
                     }
                    }
                }
            }    
        }
    }

    private void setMove(char player, int location)
    {
        mGame.setMove(player,location);
        mBoardButtons[location].setEnabled(false);
        mBoardButtons[location].setText(String.valueOf(player));
        if (player == mGame.PLAYER_ONE)
            mBoardButtons[location].setTextColor(Color.GREEN);
        else
        {
            mBoardButtons[location].setTextColor(Color.RED);
        }
    }

    @Override
    public void onClick(View view) {

        switch (view.getId())
        {
        case R.id.ten:

            startNewGame(mIsSinglePlayer);
            return;

        case R.id.eleven:

            MainActivity.this.finish();
            return;

        }
    }
}

Upvotes: 0

Views: 5046

Answers (2)

Simas
Simas

Reputation: 44158

Sounds like a project error. First follow good code guidelines.

  1. When overriding a method (e.g. onClick) add an @Override annotation before it. This will make the compiler yell, if such method doesn't exist.

  2. Specify which class your interface belongs to by using View.OnClickListener.

  3. Button already is a View, no need to cast it just to set a OnClickListener.

Here's what it might look like:

// No need to cast it to a button here, as it 
findViewById(R.id.one_player).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Log.d("DEBUG", "One Player Button Pressed!");
        Intent intent = new Intent(MainMenuScreen.this, TicTacToeGame.class);
        intent.putExtra("gameType", true);
        startActivityForResult(intent, 0);
    }
});

Lastly clean/rebuild your project as this error doesn't occur to me when using your code.

Upvotes: 1

Ridcully
Ridcully

Reputation: 23665

OnClickListener is an interface that defines the method void onClick(View v). Adding the implements OnClickListener to you Activity class means that that class implements the interface and thus must have that onClick(View view) method. This is mainly done so that you can pass this on places an OnClickListener is needed and which you also did.

But, as you coded it, you should remove the implements OnClickListenerpart from your Activity and instead pass instances of your ButtonClickListener class as OnClickListeners to your buttons like so:

Button mTen = (Button) findViewById(R.id.ten);
mTen.setOnClickListener(new ButtonClickListener(10)); /* 10 is just a guess */
Button mEleven = (Button) findViewById(R.id.eleven);
mEleven.setOnClickListener(new ButtonClickListener(11)); /* 11 is just a guess */

Upvotes: 0

Related Questions