我试图在Android工作室制作这个迷你游戏,但是当我按下任何按钮时它会一直崩溃。这是完整的Java代码,XML和清单。请知道我做错了什么?
MainActivity.java
package com.jkdevelopement.biggernumber;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
int leftRand, rightRand;
int scoreI = 0;
String leftT, rightT;
Random r = new Random();
ImageView compare;
TextView score, leftTxt, rightTxt;
Button leftBtn, rightBtn;
public int randomNum() {
final int min = 1;
final int max = 13;
int rand = r.nextInt(max - min + 1) + min;
return rand;
}
public void compare(int x, int y) {
if (x > y) {
scoreI++;
compare.setImageResource(R.drawable.bigger);
}
else if (x < y) {
scoreI--;
compare.setImageResource(R.drawable.smaller);
}
else if (x == y) {
scoreI += 0;
compare.setImageResource(R.drawable.equal);
}
score.setText("Score: " + String.valueOf(scoreI));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
leftTxt = (TextView) findViewById(R.id.leftTxt);
rightTxt = (TextView) findViewById(R.id.rightTxt);
score = (TextView) findViewById(R.id.score);
compare = (ImageView) findViewById(R.id.compare);
leftBtn = (Button) findViewById(R.id.leftButton);
rightBtn = (Button) findViewById(R.id.rightButton);
}
public void leftBtnClick(View view) {
leftRand = randomNum();
leftT = Integer.toString(leftRand);
leftTxt.setText(leftRand);
rightRand = randomNum();
rightT = Integer.toString(rightRand);
rightTxt.setText(rightT);
compare(leftRand, rightRand);
}
public void rightBtnClick(View view) {
rightRand = randomNum();
rightT = Integer.toString(rightRand);
rightTxt.setText(rightT);
leftRand = randomNum();
leftT = Integer.toString(leftRand);
leftTxt.setText(leftRand);
compare(rightRand, leftRand);
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jkdevelopement.biggernumber"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="9" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
XML代码:
<?xml version="1.0" encoding="utf-8"?>
<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="@drawable/background">
<TextView android:text="@string/myTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="32sp"
android:textColor="#ff2233"
android:textStyle="bold"
android:id="@+id/myTitle" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/instruction"
android:id="@+id/instructions"
android:layout_below="@+id/myTitle"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:textSize="16sp"
android:textAlignment="center"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/leftbtn"
android:id="@+id/leftButton"
android:onClick="leftBtnClick"
android:layout_marginTop="150dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rightBtn"
android:id="@+id/rightButton"
android:layout_alignBottom="@+id/leftButton"
android:layout_alignRight="@+id/instructions"
android:layout_alignEnd="@+id/instructions"
android:onClick="rightBtnClick"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/score"
android:id="@+id/score"
android:layout_marginTop="200dp"
android:textSize="22sp"
android:layout_below="@+id/rightButton"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/leftTxt"
android:id="@+id/leftTxt"
android:layout_below="@+id/leftButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="80dp"
android:textSize="36sp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/compare"
android:src="@drawable/equal"
android:layout_marginTop="40dp"
android:layout_below="@+id/rightButton"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/rightTxt"
android:id="@+id/rightTxt"
android:layout_alignBaseline="@+id/leftTxt"
android:layout_alignBottom="@+id/leftTxt"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="36sp"/>
</RelativeLayout>
答案 0 :(得分:0)
由于这一行,
找不到它的字符串资源异常 leftTxt.setText(leftRand);
用
更改它 leftTxt.setText(leftT);
您将整数值设置为导致异常的setText()
正确的代码如下,
public void leftBtnClick(View view) {
leftRand = randomNum();
leftT = Integer.toString(leftRand);
leftTxt.setText(leftT );
rightRand = randomNum();
rightT = Integer.toString(rightRand);
rightTxt.setText(rightT);
compare(leftRand, rightRand);
}
public void rightBtnClick(View view) {
rightRand = randomNum();
rightT = Integer.toString(rightRand);
rightTxt.setText(rightT);
leftRand = randomNum();
leftT = Integer.toString(leftRand);
leftTxt.setText(leftT);
compare(rightRand, leftRand);
}