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

我需要将一个参数从一个 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中

http://img.mukewang.com/641417420001891413260444.jpg

慕慕森
浏览 166回答 2
2回答

慕后森

注意数据类型:在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");         }然后再试一次

墨色风雨

试试这个例子<RadioGroup&nbsp; &nbsp; android:id="@+id/radioSex"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content" >&nbsp; &nbsp; <RadioButton&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/radioMale"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:text="@string/radio_male"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; android:checked="true" />&nbsp; &nbsp; <RadioButton&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/radioFemale"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:text="@string/radio_female" /></RadioGroup>在你的A班radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);btnDisplay = (Button) findViewById(R.id.btnDisplay);btnDisplay.setOnClickListener(new OnClickListener() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // get selected radio button from radioGroup&nbsp; &nbsp; &nbsp; &nbsp; int selectedId = radioSexGroup.getCheckedRadioButtonId();&nbsp; &nbsp; &nbsp; &nbsp; // find the radiobutton by returned id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; radioSexButton = (RadioButton) findViewById(selectedId);&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(MyAndroidAppActivity.this,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; radioSexButton.getText(), Toast.LENGTH_SHORT).show();&nbsp; &nbsp; }});发布崩溃报告以了解有关您的问题的更多信息
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java