为Android游戏

时间:2015-07-25 22:54:53

标签: android button textview integer increment

我是android编程的初学者,我正在尝试制作一个简单的Rock,Paper,Scissors应用程序。现在我有三个按钮(摇滚,纸和剪刀)。当用户按下它们时,计算机随机选择摇滚,纸张或剪刀,然后比较它们以查看用户是否赢了。如果用户赢了,那么我希望屏幕右上角的得分计数器增加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>

1 个答案:

答案 0 :(得分:1)

onCreate()中的这一行造成了所有麻烦:

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

您要创建并初始化局部变量,而不是初始化活动类的属性。将其更改为:

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