2014-03-28 49 views
-1

我刚开始学习如何制作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 唯一的问题是,我无法找到这个错误来自哪里。 代码是:获取资源的Android应用程序开发NotFoundException

公共类主要扩展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" /> 

那么是什么原因造成这个错误的任何想法将是一个很多帮助。并请原谅我的不好的编码,我还是新来这个:)

+0

发布完整的logcat – codeMagic

回答

2

我相信这条线的问题是:

highscore.setText(scoreCount); 

setText()方法有一个重载接受一个int指一个字符串资源。更改为:

highscore.setText("" + scoreCount); 
+0

啊,就是这样。谢谢!有没有办法看到错误在哪里?就这样,我不必花费一辈子的时间寻找它/在这里寻求找到那样的东西 –

+0

是的。它在你的logcat某处。这将是最后一行'Main.java:###',最后一行'###'是行号。我可以更具体地了解如果您花费一秒钟来运行破碎的应用程序,应该寻找什么。 –

+0

啊,我明白了。非常感谢你。 –

相关问题