Okay so I was trying to test my app and as soon as I clicked on the button to start the app I got a weird error. I have no idea how to solve this, I tried looking over my code and correcting the text view .set text but it did not work. Also my manifest has all the activities in it. Also when I use my menu activity to select the other options this error does not occur.
Here are all the things that you need
Here is the error: first is the string.xml file Then the xml for that activity then comes the java class for the activity then the main menu activity code And Finally the layout for the main menu
10-08 23:26:38.851 2567-2567/com.hfad.tictactoegame E/AndroidRuntime: FATAL EXCEPTION: main Process: com.hfad.tictactoegame, PID: 2567 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hfad.tictactoegame/com.hfad.tictactoegame.HumanVHuman}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.hfad.tictactoegame.HumanVHuman.onCreate(HumanVHuman.java:54) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 10-08 23:26:41.790 2567-2567/com.hfad.tictactoegame I/Process: Sending signal. PID: 2567 SIG: 9
<resources> <string name="app_name">TicTacToeGame</string> <string name="Main_Menu">Tic Tac Toe</string> <string name="option2">Human VS Human</string> <string name="title_activity_information">Information</string> <string name="title_activity_tic_tac_toe_info">TicTacToeInfo</string> <string name="first_human">You go first.</string> <string name="turn_player_one">Player One\'s Turn.</string> <string name="turn_player_two">Player Two\'s Turn.</string> <string name="result_tie">It\'s a tie!</string> <string name="result_player_one_wins">Player One Wins!</string> <string name="result_player_two_wins">Player Two Wins!</string> <string name="one">1</string> <string name="two">2</string> <string name="three">3</string> <string name="four">4</string> <string name="five">5</string> <string name="six">6</string> <string name="seven">7</string> <string name="eight">8</string> <string name="nine">9</string> <string name="info">Info</string> <string name="human">Human: </string> <string name="player_one">Player One: </string> <string name="ties">Ties: </string> <string name="android">Android: </string> <string name="player_two">Player Two: </string> <string name="contact_heading">Contact Info</string> <string name="newGame_label">New Game</string> <string name="hvc">Human VS Computer</string> <string name="hvh">Player One VS Player Two</string> </resources> Then the the activity layout package com.hfad.tictactoegame; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class HumanVHuman extends Activity { private TicTacToeHvH 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 mGameOver = false; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_human_vcomputer); 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); mInfoTextView = (TextView) findViewById(R.id.information); mPlayerOneCount = (TextView) findViewById(R.id.player_one_count); mTieCount = (TextView) findViewById(R.id.tiesCount); mPlayerTwoCount = (TextView) findViewById(R.id.player_two_count); mPlayerOneCount.setText(Integer.toString(mPlayerOneCounter)); mTieCount.setText(Integer.toString(mTieCounter)); mPlayerTwoCount.setText(Integer.toString(mPlayerTwoCounter)); mGame = new TicTacToeHvH(); startNewGame(); } public void startNewGame() { mGame.clearBoard(); for (int i = 0; i < mBoardButtons.length; i++) { mBoardButtons[i].setText(""); mBoardButtons[i].setEnabled(true); mBoardButtons[i].setOnClickListener(new ButtonClickListener(i)); } if (mPlayerOneFirst) { mInfoTextView.setText(R.string.turn_player_one); mPlayerOneFirst = false; } else { mInfoTextView.setText(R.string.turn_player_two); //int move = mGame.getComputerMove(); //setMove(mGame.PLAYER_TWO, move); mPlayerOneFirst = true; } } public class ButtonClickListener implements View.OnClickListener { int location; public ButtonClickListener(int location) { this.location = location; } public void onClick(View view) { if (!mGameOver) {//start if for end hame if (mBoardButtons[location].isEnabled()) {//start if for if the button is enabled if(mPlayerOneFirst) { setMove(mGame.PLAYER_ONE, location); } else { setMove(mGame.PLAYER_TWO, location); } int winner = mGame.checkForWinner(); if (winner == 0) { if(mPlayerOneFirst) { mInfoTextView.setText(R.string.turn_player_two); mPlayerOneFirst =false; //int move = mGame.getComputerMove(); //setMove(mGame.PLAYER_TWO, move); //winner = mGame.checkForWinner(); } else { mInfoTextView.setText(R.string.turn_player_one); mPlayerOneFirst =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.result_player_one_wins); mPlayerOneCounter++; mPlayerOneCount.setText(Integer.toString(mPlayerOneCounter)); mGameOver = true; } else { mInfoTextView.setText(R.string.result_player_two_wins); mPlayerTwoCounter++; mPlayerTwoCount.setText(Integer.toString(mPlayerOneCounter)); mGameOver = true; } }//end if if button is enabled }//end for game not false } } public 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.BLUE); else mBoardButtons[location].setTextColor(Color.RED); } } <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:weightSum="1" android:background="@color/black" > <TextView android:id="@+id/TitleScreen" android:text="@string/hvh" android:layout_width="314dp" android:layout_height="wrap_content" android:textColor="@color/gold" android:layout_gravity="center_horizontal" android:textSize="25dp" android:textAlignment="center" android:layout_weight="0.32" android:background="@color/brown" /> <TableLayout android:id="@+id/playArea" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" > <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" > <Button android:id="@+id/one" android:layout_width="100dp" android:layout_height="100dp" android:text="@string/one" android:textSize="70dp" /> <Button android:id="@+id/two" android:layout_width="100dp" android:layout_height="100dp" android:text="@string/two" android:textSize="70dp" /> <Button android:id="@+id/three" android:layout_width="100dp" android:layout_height="100dp" android:text="@string/three" android:textSize="70dp" /> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" > <Button android:id="@+id/four" android:layout_width="100dp" android:layout_height="100dp" android:text="@string/four" android:textSize="70dp" /> <Button android:id="@+id/five" android:layout_width="100dp" android:layout_height="100dp" android:text="@string/five" android:textSize="70dp" /> <Button android:id="@+id/six" android:layout_width="100dp" android:layout_height="100dp" android:text="@string/six" android:textSize="70dp" /> </TableRow> <TableRow android:id="@+id/tableRow3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" > <Button android:id="@+id/seven" android:layout_width="100dp" android:layout_height="100dp" android:text="@string/seven" android:textSize="70dp" /> <Button android:id="@+id/eight" android:layout_width="100dp" android:layout_height="100dp" android:text="@string/eight" android:textSize="70dp" /> <Button android:id="@+id/nine" android:layout_width="100dp" android:layout_height="100dp" android:text="@string/nine" android:textSize="70dp" /> </TableRow> </TableLayout> <TableLayout android:id="@+id/tableLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" > <TableRow android:id="@+id/tableRow4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_horizontal" > <TextView android:id="@+id/player_one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/player_one" android:textSize="25dp" android:textColor="@color/red" /> <TextView android:id="@+id/player_one_count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:textColor="@color/red" /> <TextView android:id="@+id/ties" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/ties" android:textSize="25dp" android:textColor="@color/blue" /> <TextView android:id="@+id/tiesCount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:textColor="@color/blue" /> <TextView android:id="@+id/player_two" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/player_two" android:textSize="25dp" android:textColor="@color/purple" /> <TextView android:id="@+id/player_two_count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/purple" /> </TableRow> </TableLayout> <TextView android:id="@+id/information" android:layout_width="370dp" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:gravity="center_horizontal" android:text="@string/info" android:textColor="@color/blue" android:textSize="25dp" /> <Button android:layout_width="136dp" android:layout_height="wrap_content" android:text="@string/newGame_label" android:onClick="startNewGame" android:textColor="@color/red" android:layout_weight="0.22" android:layout_gravity="center_horizontal" /> </LinearLayout> package com.hfad.tictactoegame; import java.util.Random; public class TicTacToeHvH { private char mBoard[]; private final static int BOARD_SIZE = 9; public static final char PLAYER_ONE = 'X'; public static final char PLAYER_TWO = '0'; public static final char EMPTY_SPACE = ' '; // private Random mRand; public static int getBOARD_SIZE() { // Return the size of the board return BOARD_SIZE; } public TicTacToeHvH(){ mBoard = new char[BOARD_SIZE]; for (int i = 0; i < BOARD_SIZE; i++) mBoard[i] = EMPTY_SPACE; //mRand = new Random(); } // Clear the board of all X's and O's public void clearBoard() { for (int i = 0; i < BOARD_SIZE; i++) { mBoard[i] = EMPTY_SPACE; } } public void setMove(char player, int location) {//start setMove method mBoard[location] = player; }//end setmove method public int checkForWinner() { // Check horizontal wins for (int i = 0; i <= 6; i += 3) { if (mBoard[i] == PLAYER_ONE && mBoard[i+1] == PLAYER_ONE && mBoard[i+2] == PLAYER_ONE) return 2; if (mBoard[i] == PLAYER_TWO && mBoard[i+1] == PLAYER_TWO && mBoard[i+2] == PLAYER_TWO) return 3; } // Check vertical wins for (int i = 0; i <= 2; i++) { if (mBoard[i] == PLAYER_ONE && mBoard[i+3] == PLAYER_ONE && mBoard[i+6] == PLAYER_ONE) return 2; if (mBoard[i] == PLAYER_TWO && mBoard[i+3] == PLAYER_TWO && mBoard[i+6] == PLAYER_TWO) return 3; } // Check for diagonal wins if ((mBoard[0] == PLAYER_ONE && mBoard[4] == PLAYER_ONE && mBoard[8] == PLAYER_ONE) || mBoard[2] == PLAYER_ONE && mBoard[4] == PLAYER_ONE && mBoard[6] == PLAYER_ONE) return 2; if ((mBoard[0] == PLAYER_TWO && mBoard[4] == PLAYER_TWO && mBoard[8] == PLAYER_TWO) || mBoard[2] == PLAYER_TWO && mBoard[4] == PLAYER_TWO && mBoard[6] == PLAYER_TWO) return 3; // Check for a tie for (int i = 0; i < getBOARD_SIZE(); i++) { // if we find a number, then no one has won yet if (mBoard[i] != PLAYER_ONE && mBoard[i] != PLAYER_TWO) return 0; } // If we make it through the previous loop, all places are taken, so it's a tie return 1; } } package com.hfad.tictactoegame; import android.app.Activity; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class MainMenu extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_menu); } public void StartHumanVsHuman(View view) { Intent i = new Intent(this,HumanVHuman.class); startActivity(i); } } <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:background="@color/silver" tools:context="com.hfad.tictactoegame.MainMenu"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="@string/Main_Menu" android:id="@+id/first_line" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textAllCaps="true" android:textSize="50dp" android:textColor="@color/red" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/option2" android:id="@+id/hvhoption" android:layout_centerVertical="true" android:layout_alignRight="@+id/info" android:layout_alignEnd="@+id/info" android:layout_alignLeft="@+id/first_line" android:layout_alignStart="@+id/first_line" android:textSize="20dp" android:textColor="@color/blue" android:background="@color/Yellow" android:onClick="StartHumanVsHuman" />
No comments:
Post a Comment