Reputation: 82
I'm a beginner to android programming and I'm trying make a simple Rock, Paper, Scissors app. Right now I have three buttons (rock, paper, and scissors). When the user presses them, the computer randomly picks rock, paper, or scissors and then compares them to see if the user won. If the user won, then I want the score counter to in the upper right corner of the screen to increment by 1. At first I tried to initialize int scoreCount outside of the onCreate method which resulted in the app not being able to start up. Right now, I'm getting a null pointer exception inside my upScore method where I'm trying to use setText to update my TextView with the newest scoreCount. Below is my code. Thanks for any help that you can provide! Let me know if you need additional info! I'm really stuck on this!
import android.app.DialogFragment;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
int scoreCount;
TextView scoreView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scoreCount = 0;
TextView scoreView = (TextView) findViewById(R.id.score);
scoreView.setText("Score: " + scoreCount);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void outcome(View view){
//This function will determine whether the user picked Rock, Paper, or Scissors
//and then randomly have the computer pick Rock, Paper, or Scissors. A dialog will then
//generate to let the user know whether they won or lost.
String[] rpsArray = {"Rock","Paper","Scissors"};
int index = new Random().nextInt(rpsArray.length);
String cpuChoice = rpsArray[index];
String message = "";
int id = view.getId();
switch(id){
case R.id.rock_button:
if(cpuChoice.equals("Rock")){
//Tie
message = "The Computer picked Rock! It's a Tie!";
}
else if(cpuChoice.equals("Paper")){
//You Lose
message = "The Computer picked Paper! You Lose!";
}
else if(cpuChoice.equals("Scissors")){
//You win
message = "The Computer picked Scissors! You Win!";
upScore();
}
break;
case R.id.paper_button:
if(cpuChoice.equals("Paper")){
//Tie
message = "The Computer picked Paper! It's a Tie!";
}
else if(cpuChoice.equals("Scissors")){
//You Lose
message = "The Computer picked Scissors! You Lose!";
}
else if(cpuChoice.equals("Rock")){
//You win
message = "The Computer picked Rock! You Win!";
upScore();
}
break;
case R.id.scissors_button:
if(cpuChoice.equals("Scissors")){
//Tie
message = "The Computer picked Scissors! It's a Tie!";
}
else if(cpuChoice.equals("Rock")){
//You Lose
message = "The Computer picked Rock! You Lose!";
}
else if(cpuChoice.equals("Paper")){
//You win
message = "The Computer picked Paper! You Win!";
upScore();
}
break;
}
DialogFragment dialog = WinOrLoseDialog.newInstance(message);
dialog.show(getFragmentManager(),"WinOrLose");
}
public void upScore(){
scoreCount++;
scoreView.setText("Score: " + scoreCount);
}
}
Here's my xml layout:
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="@color/dark_teal">
<Button
android:id="@+id/rock_button"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:text="@string/rock"
android:onClick="outcome" />
<Button
android:id="@+id/paper_button"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_below="@id/rock_button"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="@string/paper"
android:onClick="outcome" />
<Button
android:id="@+id/scissors_button"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_below="@id/paper_button"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="@string/scissors"
android:onClick="outcome" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/version"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="@color/white" />
<TextView
android:id="@+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textColor="@color/white"
android:textSize="25sp"
android:textStyle="bold" />
</RelativeLayout>
Upvotes: 0
Views: 1582
Reputation: 7772
This line in onCreate() is causing all your trouble:
TextView scoreView = (TextView) findViewById(R.id.score);
Instead of initializing the property of your activity class, you are creating and initializing a local variable. Change this to:
this.scoreView = (TextVoew) findViewById(R.id.score);
Upvotes: 1