为什么我的按钮没有工作?

时间:2014-11-15 21:57:55

标签: java android eclipse

所以我最近制作了我的第一个应用程序并出去测试它。当我在手机上打开应用程序时遇到了2个问题。关闭我的应用程序的菜单有3个按钮,1个用于启动" 1个玩家游戏" 1开始#2; 2人游戏"和1退出应用程序。当我按下按钮播放" 1人游戏时#34;什么都没发生。当我按下" 2玩家游戏"按钮,应用关闭错误"不幸的是testjk已关闭"。我完全不知道我在代码中做错了什么。我的问题标签中也没有错误。一些帮助将不胜感激。

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

        //1player

        findViewById(R.id.two_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);
            }
        });

        //2players
        findViewById(R.id.two_player).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Log.d("DEBUG", "Two Player Button Pressed!");
                Intent intent = new Intent(MainMenuScreen.this, TicTacToeGame.class);
                intent.putExtra("gameType", false);
                startActivityForResult(intent, 0);
            }
        });

        //exit game
        findViewById(R.id.exit_game).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                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)
    {
        this.mIsSinglePlayer = isSingle;

        mGame.clearBoard();

        for (int i = 0; i < mBoardButtons.length; i++)
        {
            mBoardButtons[i].setText("");
            mBoardButtons[i].setEnabled(true);
            mBoardButtons[i].setOnClickListener(new ButtonClickListener(i));

        }
        if (mIsSinglePlayer)
        {
             mPlayeroneText.setText("speler: ");
             mPlayertwoText.setText("android: ");

             if (mPlayeroneFirst)
             {
                 mInfoTextView.setText(R.string.first_human);
                 mPlayeroneFirst = false;
             }
             else
             {
                 mInfoTextView.setText(R.string.turn_computer);
                 int move = mGame.getComputerMove();
                 setMove(TicTacToeGame.PLAYER_TWO, move);
                 mPlayeroneFirst = true;
             }
        }
        else
        {
             mPlayeroneText.setText("speler 1: ");
             mPlayertwoText.setText("speler 2: ");

             if (mPlayeroneFirst)
             {
                 mInfoTextView.setText(R.string.turn_player_one);
                 mPlayeroneFirst = false;
             }
             else
             {
                 mInfoTextView.setText(R.string.turn_player_two);
                 mPlayeroneFirst = true;
             }

        }

        mGameOver = false;
    }

    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(TicTacToeGame.PLAYER_ONE, location);

                         int winner = mGame.checkForWinner();

                         if (winner == 0)
                         {
                             mInfoTextView.setText(R.string.turn_computer);
                             int move = mGame.getComputerMove();
                             setMove(TicTacToeGame.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(TicTacToeGame.PLAYER_ONE, location);
                        }
                        else
                        {
                            setMove(TicTacToeGame.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);
        if (player == TicTacToeGame.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;

        }
    }
}

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.wouter.testjk"
    android:versionCode="3"
    android:versionName="3" >


    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainMenuScreen"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" > 
        </activity>
    </application>

</manifest>

LogCat:http://textuploader.com/on1k --------  http://imgur.com/d8662NO ----------- 我不知道我应该如何上传这个

1 个答案:

答案 0 :(得分:4)

您要将OnClickListener分配给同一个按钮R.id.two_player。也就是说,将第一个更改为one_player,如

// 1player
findViewById(R.id.one_player).setOnClickListener(new View.OnClickListener() {
    // (...)
});

//2players
findViewById(R.id.two_player).setOnClickListener(new View.OnClickListener() {
    // (...)
});

编辑:正如我在logcat中看到的,并且Spidy在评论中也正确指出,您需要在清单中声明您的活动。

11-15 23:45:28.227: E/AndroidRuntime(6056): FATAL EXCEPTION: main
11-15 23:45:28.227: E/AndroidRuntime(6056): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.wouter.testjk/com.wouter.testjk.TicTacToeGame}; have you declared this activity in your AndroidManifest.xml?

EDIT2 :从日志中可以清楚地看到:

11-16 00:39:09.867: E/AndroidRuntime(7124): FATAL EXCEPTION: main
11-16 00:39:09.867: E/AndroidRuntime(7124): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.wouter.testjk/com.wouter.testjk.TicTacToeGame}: java.lang.ClassCastException: com.wouter.testjk.TicTacToeGame cannot be cast to android.app.Activity

您正在尝试将类TicTacToeGame实例化为活动,但事实并非如此。我猜你想去MainActivity。在这种情况下,您必须将您的意图更改为此类

Intent intent = new Intent(MainMenuScreen.this, MainActivity.class);

希望它有所帮助。