我正在开发一个儿童学习开发应用程序,我希望在屏幕顶部有一个分数,向他们展示他们到目前为止有多少问题。我有一个if声明,声明如果他们用正确的字母拼写答案然后他们被转移到下一个活动但是当他们移动时我希望他们的分数更新。希望通过意图将计数器传递给第二个活动,但是当我单击提交按钮时,应用程序崩溃了。 这是第一堂课
package com.helloworld.erica.alphaanimals;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import static com.helloworld.erica.alphaanimals.R.id.imageView;
public class button_a extends Activity {
private Button button_b;
TextView scoreText;
int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.button_a);
//final TextView score = (TextView) findViewById(R.id.score);
final ImageView ImageView = (ImageView) findViewById(imageView);
Button submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
EditText answerA = (EditText) findViewById(R.id.answerA);
String toCompare = answerA.getText().toString();
TextView score = (TextView)findViewById(R.id.score);
if(toCompare.startsWith("A")) {
Intent intent_b = new Intent(button_a.this, button_b.class);
intent_b.putExtra("username", score.getText().toString());
startActivity(intent_b);
counter++;
scoreText.setText(Integer.toString(counter));
Toast.makeText(button_a.this, "Well Done, correct answer!!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(button_a.this, "Incorrect", Toast.LENGTH_SHORT).show();
}
}
});
}
} 这是第二个
package com.helloworld.erica.alphaanimals;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class button_b extends Activity {
private Button button_c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.button_b);
final ImageView ImageView = (ImageView) findViewById(imageView);
Button submit = (Button) findViewById(R.id.submit);
TextView scoreText = (TextView) findViewById(R.id.scoreText);
scoreText.setText(getIntent().getExtras().getString("username"));
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
EditText answerB = (EditText) findViewById(R.id.answerB);
String toCompare = answerB.getText().toString();
if(toCompare.startsWith("B")) {
Intent intent_c = new Intent(button_b.this, button_c.class);
startActivity(intent_c);
//ImageView.setImageResource(R.drawable.bear);
Toast.makeText(button_b.this, "Well Done, correct answer!!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(button_b.this, "Incorrect", Toast.LENGTH_SHORT).show();
}
}
});
}
}
答案 0 :(得分:0)
您在设置之前获取文本,以便进行以下更改:
counter++;
scoreText.setText(Integer.toString(counter));
intent_b.putExtra("username", score.getText().toString());
startActivity(intent_b);
答案 1 :(得分:0)
您应该检查Android Studio中Android监视器上的错误,将显示错误,您可以将其粘贴到此处。它将有助于解决您的问题。
答案 2 :(得分:0)
if(toCompare.startsWith("A")) {
counter++;
score.setText(Integer.toString(counter));
Toast.makeText(button_a.this, "Well Done, correct answer!!", Toast.LENGTH_SHORT).show();
Intent intent_b = new Intent(button_a.this, button_b.class);
intent_b.putExtra("username", score.getText().toString());
startActivity(intent_b);
}else{
Toast.makeText(button_a.this, "Incorrect", Toast.LENGTH_SHORT).show();
}
希望这有助于你
答案 3 :(得分:0)
第一项活动中没有scoreText。您的TextView名称是得分,因此更改它以及语句的顺序不正确。 Intent应该在counter ++之后并在TextView中设置文本。