新的View.OnClickListener类型必须实现继承的抽象方法

时间:2014-11-15 16:01:03

标签: java android eclipse inheritance

所以我刚刚开始编程,但我真的没有得到OnClickListener方法。我已经知道了我的错误:“新的View.OnClickListener(){}类型必须实现继承的抽象方法View.OnClickListener.OnClick(View)”并且我尝试更改代码中的一些内容,但它仍然会执行工作。任何人都可以告诉我我的代码中需要更改的内容吗?提前谢谢。

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;

        }
    }
}

2 个答案:

答案 0 :(得分:1)

听起来像项目错误。首先遵循良好的代码指南。

  1. 覆盖方法(例如onClick)时,在其前面添加@Override注释。如果这种方法不存在,这将使编译器大叫。

  2. 使用View.OnClickListener指定您的界面所属的类。

  3. Button已经是View,无需仅仅为了设置OnClickListener而投出它。

  4. 以下是它的样子:

    // 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);
        }
    });
    

    最后清理/重建您的项目,因为在使用您的代码时我不会发生此错误。

答案 1 :(得分:0)

OnClickListener界面,用于定义方法void onClick(View v)。将implements OnClickListener添加到Activity类意味着该类实现了接口,因此必须具有onClick(View view)方法。这主要是为了让您可以在需要this的地方传递OnClickListener,而且您也可以这样做。

但是,在编写代码时,您应该从Activity中删除implements OnClickListener部分,而是将ButtonClickListener类的实例作为OnClickListeners传递给按钮,如下所示:

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 */