我需要将一个参数从一个 Java 类 (A) 传递到另一个 Java 类 (B)。我使用了互联网上的许多解决方案,但都无法解决我的问题。用户将从单选按钮列表中选择他们的答案。分数会加起来,我需要把分数传给B班。
在B类中,用户继续回答问题,我需要将A类和B类的分数相加得到最终分数,并将其显示在B类的底部。当我点击 A 类中的按钮时,应用程序保持停止。但我认为问题出在B类。有谁知道如何解决这个问题?太感谢了。
一类
private int score;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
Button btn = findViewById(R.id.anxnext);
final RadioGroup rg1 = findViewById(R.id.anxq1g);
final RadioGroup rg2 = findViewById(R.id.anxq2g);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get the checked Radio Button ID from Radio Group
int g1 = rg1.getCheckedRadioButtonId();
int g2 = rg2.getCheckedRadioButtonId();
if (g1 != -1) {
View radioButton = rg1.findViewById(g1);
idx1 = rg1.indexOfChild(radioButton);
}
if (g2 != -1) {
View radioButton = rg2.findViewById(g2);
idx2 = rg2.indexOfChild(radioButton);
}
score=idx1+idx2;
Intent intent = new Intent(A.this, B.class);
intent.putExtra("message", score);
startActivity(intent);
}
});
}
B类
private int score1,totalscore;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.b);
Bundle extras = getIntent().getExtras();
if(extras!=null) {
String m= extras.getString("message");
totalscore=Integer.parseInt(m);
}
下面显示在logcat中
慕后森
墨色风雨
相关分类