Java Android应用程序开发获取resourceNotFoundException
我刚刚开始学习如何制作一款android应用程序,在我第一次尝试时,我决定制作一款Tic-Tac-Toe游戏。不幸的是,每当我试图在模拟器上运行应用程序时,它都会崩溃。我检查了日志,我已经将崩溃缩小到这个错误 :Java Android应用程序开发获取resourceNotFoundException,java,android,eclipse,resources,adk,Java,Android,Eclipse,Resources,Adk,我刚刚开始学习如何制作一款android应用程序,在我第一次尝试时,我决定制作一款Tic-Tac-Toe游戏。不幸的是,每当我试图在模拟器上运行应用程序时,它都会崩溃。我检查了日志,我已经将崩溃缩小到这个错误 : 03-28 19:23:45.385:E/AndroidRuntime(1095):由以下原因引起:android.content.res.Resources$NotFoundException:字符串资源ID#0x0 唯一的问题是,我找不到这个错误的来源。 代码是: 公共类Main
03-28 19:23:45.385:E/AndroidRuntime(1095):由以下原因引起:android.content.res.Resources$NotFoundException:字符串资源ID#0x0
唯一的问题是,我找不到这个错误的来源。
代码是:
公共类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" />
因此,任何关于导致此错误的原因的想法都会有很大帮助。请原谅我的错误编码,我对此仍然是新手:)我相信这一行就是问题所在:
highscore.setText(scoreCount);
setText()
方法有一个重载,该重载接受指向字符串资源的int。将其更改为:
highscore.setText("" + scoreCount);
啊,就是这样。谢谢!有没有办法知道错误在哪里?这样我就不必花太多时间在这里寻找它/要求在这里找到像那样少的东西是的。它就在你的日志中的某个地方。它将是最后一行有
Main.java:####
结尾,其中####
是行号。我可以更具体地说但是如果你给我一秒钟来运行一个坏掉的应用程序,你会发现什么。啊,我知道了。非常感谢。