为了账号安全,请及时绑定邮箱和手机立即绑定

如何将信息从一个 java 类传递到另一个

如何将信息从一个 java 类传递到另一个

慕慕森 2023-03-17 15:30:23
我需要将一个参数从一个 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中
查看完整描述

2 回答

?
慕后森

TA贡献1802条经验 获得超5个赞

注意数据类型:

在A班,你有:

score=idx1+idx2; 
       Intent intent = new Intent(A.this, B.class);
                intent.putExtra("message", score);
                startActivity(intent);

score是 int,但是在 B 类中:

Bundle extras = getIntent().getExtras(); 
       if(extras!=null) {
            String m= extras.getString("message");
            totalscore=Integer.parseInt(m);
        }

您正试图将其作为字符串获取,但它为空,因此应用程序崩溃了。

所以请将其更改为:

Bundle extras = getIntent().getExtras(); 
       if(extras!=null) {
            totalscore = extras.getInt("message");
        }

然后再试一次


查看完整回答
反对 回复 2023-03-17
?
墨色风雨

TA贡献1853条经验 获得超6个赞

试试这个例子

<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>

在你的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();


    }


});

发布崩溃报告以了解有关您的问题的更多信息


查看完整回答
反对 回复 2023-03-17
  • 2 回答
  • 0 关注
  • 165 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号