获得resourceNotFoundException的Android应用开发

时间:2014-03-28 23:49:27

标签: java android eclipse resources adk

我刚刚开始学习如何制作一个Android应用程序,而且我自己第一次尝试,我决定制作一个Tic Tac Toe游戏。不幸的是,每当我尝试在模拟器上运行应用程序时,它都会崩溃。我检查了logcat,我已经将崩溃范围缩小到了这个错误 : 03-28 19:23:45.385: E/AndroidRuntime(1095): Caused by:android.content.res.Resources$NotFoundException: String resource ID #0x0 唯一的问题是,我无法找到此错误的来源。 代码是:

public class Main扩展ActionBarActivity {

TextView compChoice, highscore;
Button rock, paper, scissors;
int scoreCount = 0;
boolean win = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_main);
    compChoice = (TextView) findViewById(R.id.compChoice);
    highscore = (TextView) findViewById(R.id.highscore);
    rock = (Button) findViewById(R.id.rock);
    paper = (Button) findViewById(R.id.paper);
    scissors = (Button) findViewById(R.id.scissors);

    rock.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            boolean won = roll(0);
            if (won) {
                scoreCount++;
            }
            else{
                scoreCount = 0;
            }
        }
    });
    paper.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            boolean won=roll(1);
            if (won) {
                scoreCount++;
            }
            else{
                scoreCount = 0;
            }
        }
    });
    scissors.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            boolean won= roll(2);
            if (won) {
                scoreCount++;
            }
            else{
                scoreCount = 0;
            }
        }
    });
    highscore.setText(scoreCount);
}

public boolean roll(int choice) {
    int comp = (int) (Math.random()*3);
    if(choice == 0 && comp == 2){
        win = true;
        compChoice.setText("Scissors");
    }
    if(choice == 1 && comp == 0){
        win = true;
        compChoice.setText("Rock");
    }
    if(choice == 2 && comp == 1){
        win = true;
        compChoice.setText("Paper");
    }
    if (choice == 0 &&comp==1) {
        win=false;
        compChoice.setText("Paper");
    }
    if (choice == 1 &&comp==2) {
        win=false;
        compChoice.setText("Scissors");
    }
    if (choice == 2 &&comp==0) {
        win=false;
        compChoice.setText("Rock");
    }
    return win;
}

我的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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:textAlignment="center"
tools:context="com.hosfordryan.tictactoe.Main$PlaceholderFragment" >

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="Tic Tac Toe"
    android:textSize="45sp" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/title"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="16dp"
    android:text="Choose either Rock, Paper, or Scissors!"
    android:textAlignment="center"
    android:textSize="20sp" />

<Button
    android:id="@+id/rock"
    android:layout_width="90sp"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/paper"
    android:layout_alignBottom="@+id/paper"
    android:layout_toLeftOf="@+id/paper"
    android:text="Rock"
    android:textSize="15sp" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:textSize="20sp"
    android:text="Computer chose:" />

<Button
    android:id="@+id/paper"
    android:layout_width="90sp"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView3"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="18dp"
    android:text="Paper"
    android:textSize="15sp" />

<Button
    android:id="@+id/scissors"
    android:layout_width="90sp"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/paper"
    android:layout_alignBottom="@+id/paper"
    android:layout_toRightOf="@+id/paper"
    android:text="Scissors"
    android:textSize="15sp" />

<TextView
    android:id="@+id/scoreprompt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/highscore"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="15dp"
    android:text="High Score:"
    android:textSize="20sp" />

<TextView
    android:id="@+id/highscore"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="64dp"
    android:text="0"
    android:textSize="20sp" />

<TextView
    android:id="@+id/compChoice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView3"
    android:layout_centerHorizontal="true"
     android:textSize="25sp"
    android:layout_marginTop="28dp"
    android:text="Null" />

因此,任何导致此错误的想法都会有很多帮助。请原谅我的错误编码,我还是新手:)

1 个答案:

答案 0 :(得分:2)

我相信这一行是问题所在:

highscore.setText(scoreCount);

setText()方法有一个重载,它接受一个引用String资源的int。将其更改为:

highscore.setText("" + scoreCount);