android 应用程序执行期间出错
Errors during the execution of android application
我即将创建我的第一个 android 应用程序
:This is a screenshot of how the app looks
所以这个应用的主要思想是在两个按钮中生成随机数
如果他猜对了,用户猜哪个数字更大:
这是XML布局
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/app_name"
android:gravity="center_horizontal"
android:text="Press the button of the larger Number.if you get it right you will earn point !if you get it wrong you'll lose a point"
android:textSize="16sp" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/left_button_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textStyle="bold" />
<Button
android:id="@+id/right_button_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="0"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="top"
android:layout_marginBottom="57dp"
android:gravity="center_horizontal"
android:text="points : "
android:textStyle="italic" />
<TextView
android:id="@+id/score_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView"
android:layout_toEndOf="@+id/textView"
android:layout_toRightOf="@+id/textView"
android:text="0" />
<Button
android:id="@+id/reset_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout"
android:layout_centerHorizontal="true"
android:text="Reset" />
这是Java代码
public class MainActivity extends AppCompatActivity {
int leftNumber;
int rightNumber;
private OnClickListener ButtonClickListener = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.left_button_number:
if (rightNumber < leftNumber)
updateTheScore(1);
else
updateTheScore(-1);
break;
case R.id.right_button_number:
if (rightNumber > leftNumber)
updateTheScore(1);
else
updateTheScore(-1);
break;
case R.id.reset_button:
resetTheScore();
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
numberGenerator();
Button rightButton = (Button) findViewById(R.id.right_button_number);
Button leftButton = (Button) findViewById(R.id.left_button_number);
Button resetButton = (Button) findViewById(R.id.reset_button);
rightButton.setOnClickListener(ButtonClickListener);
leftButton.setOnClickListener(ButtonClickListener);
resetButton.setOnClickListener(ButtonClickListener);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
public void updateTheScore(int n) {
TextView scoreView = (TextView) findViewById(R.id.score_text_view);
int score = Integer.parseInt(scoreView.getText().toString());
score += n;
scoreView.setText(score);
numberGenerator();
}
public void resetTheScore() {
TextView scoreView = (TextView) findViewById(R.id.score_text_view);
scoreView.setText(0);
numberGenerator();
}
public void numberGenerator() {
int min = 1;
int max = 100;
Random r = new Random();
leftNumber = r.nextInt(max - min + 1) + min;
rightNumber = r.nextInt(max - min + 1) + min;
displayMessageOnButton(leftNumber, rightNumber);
}
public void displayMessageOnButton(int left, int right) {
Button rightButton = (Button) findViewById(R.id.right_button_number);
Button leftButton = (Button) findViewById(R.id.left_button_number);
rightButton.setText(right);
leftButton.setText(left);
}
这是logcat
String resource ID #0x4a
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
at android.app.ActivityThread.access0(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4945)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x4a
at android.content.res.Resources.getText(Resources.java:266)
at android.widget.TextView.setText(TextView.java:3627)
at com.example.android.big.MainActivity.displayMessageOnButton(MainActivity.java:87)
at com.example.android.big.MainActivity.numberGenerator(MainActivity.java:81)
at com.example.android.big.MainActivity.onCreate(MainActivity.java:39)
at android.app.Activity.performCreate(Activity.java:4531)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
at android.app.ActivityThread.access0(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4945)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
public void updateTheScore(int n) {
TextView scoreView = (TextView) findViewById(R.id.score_text_view);
int score = Integer.parseInt(scoreView.getText().toString());
score += n;
scoreView.setText(score); // <---- this isn't working
numberGenerator();
}
您将一个整数作为参数传递给了您的 scroteView.setText(); .在这种情况下,int 被解释为资源 ID(这显然不是您想要的)。
一个简单的
scoreView.setText(score + "");
(隐式字符串转换)将修复它。
public void numberGenerator() {
int min = 1;
int max = 100;
Random r = new Random();
leftNumber = r.nextInt((max - min) + 1) + min;
rightNumber = r.nextInt((max - min) + 1) + min;
displayMessageOnButton(leftNumber, rightNumber);
}
无法将整数设置为文本
public void displayMessageOnButton(int left, int right) {
Button rightButton = (Button) findViewById(R.id.right_button_number);
Button leftButton = (Button) findViewById(R.id.left_button_number);
rightButton.setText(right+"");
leftButton.setText(left+"");
}
我即将创建我的第一个 android 应用程序 :This is a screenshot of how the app looks
所以这个应用的主要思想是在两个按钮中生成随机数 如果他猜对了,用户猜哪个数字更大:
这是XML布局
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/app_name"
android:gravity="center_horizontal"
android:text="Press the button of the larger Number.if you get it right you will earn point !if you get it wrong you'll lose a point"
android:textSize="16sp" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/left_button_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textStyle="bold" />
<Button
android:id="@+id/right_button_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="0"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="top"
android:layout_marginBottom="57dp"
android:gravity="center_horizontal"
android:text="points : "
android:textStyle="italic" />
<TextView
android:id="@+id/score_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView"
android:layout_toEndOf="@+id/textView"
android:layout_toRightOf="@+id/textView"
android:text="0" />
<Button
android:id="@+id/reset_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout"
android:layout_centerHorizontal="true"
android:text="Reset" />
这是Java代码
public class MainActivity extends AppCompatActivity {
int leftNumber;
int rightNumber;
private OnClickListener ButtonClickListener = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.left_button_number:
if (rightNumber < leftNumber)
updateTheScore(1);
else
updateTheScore(-1);
break;
case R.id.right_button_number:
if (rightNumber > leftNumber)
updateTheScore(1);
else
updateTheScore(-1);
break;
case R.id.reset_button:
resetTheScore();
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
numberGenerator();
Button rightButton = (Button) findViewById(R.id.right_button_number);
Button leftButton = (Button) findViewById(R.id.left_button_number);
Button resetButton = (Button) findViewById(R.id.reset_button);
rightButton.setOnClickListener(ButtonClickListener);
leftButton.setOnClickListener(ButtonClickListener);
resetButton.setOnClickListener(ButtonClickListener);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
public void updateTheScore(int n) {
TextView scoreView = (TextView) findViewById(R.id.score_text_view);
int score = Integer.parseInt(scoreView.getText().toString());
score += n;
scoreView.setText(score);
numberGenerator();
}
public void resetTheScore() {
TextView scoreView = (TextView) findViewById(R.id.score_text_view);
scoreView.setText(0);
numberGenerator();
}
public void numberGenerator() {
int min = 1;
int max = 100;
Random r = new Random();
leftNumber = r.nextInt(max - min + 1) + min;
rightNumber = r.nextInt(max - min + 1) + min;
displayMessageOnButton(leftNumber, rightNumber);
}
public void displayMessageOnButton(int left, int right) {
Button rightButton = (Button) findViewById(R.id.right_button_number);
Button leftButton = (Button) findViewById(R.id.left_button_number);
rightButton.setText(right);
leftButton.setText(left);
}
这是logcat
String resource ID #0x4a
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
at android.app.ActivityThread.access0(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4945)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x4a
at android.content.res.Resources.getText(Resources.java:266)
at android.widget.TextView.setText(TextView.java:3627)
at com.example.android.big.MainActivity.displayMessageOnButton(MainActivity.java:87)
at com.example.android.big.MainActivity.numberGenerator(MainActivity.java:81)
at com.example.android.big.MainActivity.onCreate(MainActivity.java:39)
at android.app.Activity.performCreate(Activity.java:4531)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
at android.app.ActivityThread.access0(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4945)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
public void updateTheScore(int n) {
TextView scoreView = (TextView) findViewById(R.id.score_text_view);
int score = Integer.parseInt(scoreView.getText().toString());
score += n;
scoreView.setText(score); // <---- this isn't working
numberGenerator();
}
您将一个整数作为参数传递给了您的 scroteView.setText(); .在这种情况下,int 被解释为资源 ID(这显然不是您想要的)。 一个简单的
scoreView.setText(score + "");
(隐式字符串转换)将修复它。
public void numberGenerator() {
int min = 1;
int max = 100;
Random r = new Random();
leftNumber = r.nextInt((max - min) + 1) + min;
rightNumber = r.nextInt((max - min) + 1) + min;
displayMessageOnButton(leftNumber, rightNumber);
}
无法将整数设置为文本
public void displayMessageOnButton(int left, int right) {
Button rightButton = (Button) findViewById(R.id.right_button_number);
Button leftButton = (Button) findViewById(R.id.left_button_number);
rightButton.setText(right+"");
leftButton.setText(left+"");
}