android 游戏的按钮点击得分增加

Incrementing score on button click for android game

我是 android 编程的初学者,我正在尝试制作一个简单的石头剪刀布应用。现在我有三个按钮(石头、布和剪刀)。当用户按下它们时,计算机会随机选择石头、布或剪刀,然后将它们进行比较,看用户是否赢了。如果用户赢了,那么我希望屏幕右上角的分数计数器增加 1。起初我尝试在 onCreate 方法之外初始化 int scoreCount,导致应用程序无法启动。现在,我在我的 upScore 方法中遇到了一个空指针异常,我正在尝试使用 setText 使用最新的 scoreCount 更新我的 TextView。下面是我的代码。感谢您提供的任何帮助!如果您需要其他信息,请告诉我!我真的坚持这个!

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

这是我的 xml 布局:

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

onCreate() 中的这一行给您带来了所有麻烦:

TextView scoreView = (TextView) findViewById(R.id.score);

您没有初始化 activity class 的 属性,而是创建并初始化了局部变量。将其更改为:

this.scoreView = (TextVoew) findViewById(R.id.score);