嘿所以我正在建立一本教育书籍的数学游戏。我问了一个类似的问题,卸载然后重新安装android帮助,相同的代码运行。现在,我在代码中得到了进一步的帮助,似乎无法找到错误。我觉得可能是我的setQuestion()方法放在GameActivity.java的onClick方法中。此外,分数和级别元素不会更新,不确定问题是否属于该部分,但我觉得代码在更新之前就崩溃了。如果运行(在模拟器和我的手机上),直到用户选择一个答案,然后它崩溃。这是我的代码:
activity_main.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.baylamafia.snzyt.mathgamechapter2.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="My Math Game"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30sp" />
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="@+id/imageView"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:id="@+id/buttonPlay"
android:layout_centerVertical="true"
android:layout_alignEnd="@+id/button2"
android:layout_alignStart="@+id/button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="High Scores"
android:id="@+id/button2"
android:layout_below="@+id/buttonPlay"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quit"
android:id="@+id/button3"
android:layout_below="@+id/button2"
android:layout_alignStart="@+id/button2"
android:layout_alignEnd="@+id/button2" />
MainActivity.java
package com.baylamafia.snzyt.mathgamechapter2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonPlay = (Button)findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Intent i;
i = new Intent(this, GameActivity.class);
startActivity(i);
}
}
activity_game.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.baylamafia.snzyt.mathgamechapter2.GameActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="2"
android:id="@+id/textPartA"
android:textSize="70sp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="@+id/textOperator" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="x"
android:id="@+id/textOperator"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="70sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="2"
android:id="@+id/textPartB"
android:textSize="70sp"
android:layout_alignTop="@+id/textOperator"
android:layout_alignParentEnd="true"
android:layout_alignBottom="@+id/textOperator" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="="
android:id="@+id/textView2"
android:textIsSelectable="true"
android:layout_below="@+id/textOperator"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:id="@+id/buttonChoice1"
android:layout_alignTop="@+id/buttonChoice2"
android:layout_toStartOf="@+id/buttonChoice2"
android:textSize="40sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:id="@+id/buttonChoice2"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="164dp"
android:textSize="40sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:id="@+id/buttonChoice3"
android:layout_alignTop="@+id/buttonChoice2"
android:layout_toEndOf="@+id/buttonChoice2"
android:textSize="40sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Score: 999"
android:id="@+id/textScore"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Level: 4"
android:id="@+id/textLevel"
android:layout_alignTop="@+id/textScore"
android:layout_alignEnd="@+id/textPartB" />
GameActivity.java
package com.baylamafia.snzyt.mathgamechapter2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.util.Random;
public class GameActivity extends AppCompatActivity implements View.OnClickListener{
int correctAnswer;
Button buttonObjectChoice1;
Button buttonObjectChoice2;
Button buttonObjectChoice3;
TextView textObjectPartA;
TextView textObjectPartB;
TextView textObjectScore;
TextView textObjectLevel;
int currentScore = 0;
int currentLevel = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
/*here we get a working object based on either the button or TextView class and base as well
as link our new objects directly to the appropriate UI elements that we created previously
*/
textObjectPartA =
(TextView)findViewById(R.id.textPartA);
textObjectPartB =
(TextView)findViewById(R.id.textPartB);
buttonObjectChoice1 =
(Button)findViewById(R.id.buttonChoice1);
buttonObjectChoice2 =
(Button)findViewById(R.id.buttonChoice2);
buttonObjectChoice3 =
(Button)findViewById(R.id.buttonChoice3);
buttonObjectChoice1.setOnClickListener(this);
buttonObjectChoice2.setOnClickListener(this);
buttonObjectChoice3.setOnClickListener(this);
setQuestion();
}
void setQuestion(){
//generate the parts of the question
int numberRange = currentLevel * 3;
Random randInt = new Random();
int partA = randInt.nextInt(numberRange);
partA++;
int partB = randInt.nextInt(numberRange);
partB++;
correctAnswer = partA * partB;
int wrongAnswer1 = correctAnswer-2;
int wrongAnswer2 = correctAnswer+2;
textObjectPartA.setText(""+partA);
textObjectPartB.setText(""+partB);
//set the multi choice button
int buttonLayout = randInt.nextInt(3);
switch (buttonLayout){
case 0:
buttonObjectChoice1.setText(""+correctAnswer);
buttonObjectChoice2.setText(""+wrongAnswer1);
buttonObjectChoice3.setText(""+wrongAnswer2);
break;
case 1:
buttonObjectChoice1.setText(""+wrongAnswer1);
buttonObjectChoice2.setText(""+correctAnswer);
buttonObjectChoice3.setText(""+wrongAnswer2);
break;
case 2:
buttonObjectChoice1.setText(""+wrongAnswer1);
buttonObjectChoice2.setText(""+wrongAnswer2);
buttonObjectChoice3.setText(""+correctAnswer);
break;
}
}
void updateScoreAndLevel (int answerGiven) {
if(isCorrect(answerGiven)){
for(int i = 1; i < currentLevel; i++){
currentScore = currentScore + i;
}
currentLevel++;
}else{
currentScore = 0;
currentLevel = 1;
}
textObjectScore.setText("Score: " + currentScore);
textObjectLevel.setText("Level: " + currentLevel);
}
boolean isCorrect(int answerGiven){
boolean correctTrueOrFalse;
if (answerGiven == correctAnswer){
Toast.makeText(getApplicationContext(), "Well done!", Toast.LENGTH_LONG).show();
correctTrueOrFalse=true;
}else{
Toast.makeText(getApplicationContext(), "Sorry", Toast.LENGTH_LONG).show();
correctTrueOrFalse=false;
}
return correctTrueOrFalse;
}
@Override
public void onClick(View view){
int answerGiven = 0;
switch (view.getId()) {
case R.id.buttonChoice1:
answerGiven = Integer.parseInt("" + buttonObjectChoice1.getText());
break;
case R.id.buttonChoice2:
answerGiven = Integer.parseInt("" + buttonObjectChoice2.getText());
break;
case R.id.buttonChoice3:
answerGiven = Integer.parseInt("" + buttonObjectChoice3.getText());
break;
}
updateScoreAndLevel(answerGiven);
setQuestion();
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.baylamafia.snzyt.mathgamechapter2">
<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>
<activity android:name=".GameActivity"></activity>
</application>
答案 0 :(得分:0)
请勿在此处使用getApplicationContext
:
Toast.makeText(getApplicationContext(), "Well done!", Toast.LENGTH_LONG).show();
使用this
来使用当前活动:
Toast.makeText(this, "Well done!", Toast.LENGTH_LONG).show();
Difference between getContext() , getApplicationContext() , getBaseContext() and "this"
对于初学者来说,摆脱所有这些无关紧要的""+
他们没有任何目的。您没有向字符串添加任何内容:
buttonObjectChoice1.setText(""+correctAnswer);
answerGiven = Integer.parseInt("" + buttonObjectChoice1.getText());
也是这个
answerGiven = Integer.parseInt("" + buttonObjectChoice1.getText());
如果没有正确的输入,即<。> ,将失败
此外,我不确定您在import org.w3c.dom.Text
使用此内容。