[英]How to pass information from one java class to another
I need to pass one parameter from one Java class(A) to another Java class(B). 我需要将一个参数从一个Java类(A)传递给另一Java类(B)。 I use many solutions from the Internet but it couldn't solve my problem.
我使用了许多来自Internet的解决方案,但无法解决我的问题。 The user will choose their answer from a list of radio button.
用户将从单选按钮列表中选择答案。 The score will be added and I need to pass the score to B class.
分数将被添加,我需要将分数传递给B类。
In B class, the user continues to answer the question and I need to add the score from both A and B class to get the final score and display it at the bottom of B class. 在B类中,用户继续回答该问题,我需要添加A类和B类的分数以获得最终分数并将其显示在B类的底部。 The application keep stopped when I click on the button in A class.
当我单击A类中的按钮时,应用程序保持停止状态。 But I think the problem is in B class.
但我认为问题出在B级。 Does anyone know how to solve this?
有谁知道如何解决这个问题? Thank you so much.
非常感谢。
A class 一类
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 class 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);
}
Button btn = findViewById(R.id.anxresult);
final TextView tv_result = findViewById(R.id.tv_result);
final RadioGroup rg10 = findViewById(R.id.anxq10g);
final RadioGroup rg11 = findViewById(R.id.anxq11g);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get the checked Radio Button ID from Radio Grou[
int g1 = rg10.getCheckedRadioButtonId();
int g2 = rg11.getCheckedRadioButtonId();
if (g1 != -1) {
View radioButton = rg10.findViewById(g1);
idx10 = rg10.indexOfChild(radioButton);
}
if (g2 != -1) {
View radioButton = rg11.findViewById(g2);
idx11 = rg11.indexOfChild(radioButton);
}
score1 = idx10 + idx11;
totalscore = score1 + totalscore;
tv_result.setText(totalscore + " selected.");
}
});
}
<RadioGroup
android:id="@+id/radioSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_male"
android:checked="true" />
<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_female" />
</RadioGroup>
In your class A 在你的A班
radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// get selected radio button from radioGroup
int selectedId = radioSexGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioSexButton = (RadioButton) findViewById(selectedId);
Toast.makeText(MyAndroidAppActivity.this,
radioSexButton.getText(), Toast.LENGTH_SHORT).show();
}
});
post the crash report to know more about your issue 发布崩溃报告以了解有关您的问题的更多信息
Take care of the data type: 注意数据类型:
In A class, you have: 在A类中,您具有:
score=idx1+idx2;
Intent intent = new Intent(A.this, B.class);
intent.putExtra("message", score);
startActivity(intent);
which score
is int, however in B class: 哪个
score
是整数,但是在B类中:
Bundle extras = getIntent().getExtras();
if(extras!=null) {
String m= extras.getString("message");
totalscore=Integer.parseInt(m);
}
You are trying to get it as a String, and it is null so the app crashes. 您正在尝试将其作为字符串获取,并且它为null,因此应用程序崩溃。
So please change it to: 因此,请将其更改为:
Bundle extras = getIntent().getExtras();
if(extras!=null) {
totalscore = extras.getInt("message");
}
And try again 然后再试一次
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.