【问题标题】:How to pass information from one java class to another如何将信息从一个 java 类传递到另一个
【发布时间】:2019-11-19 22:18:16
【问题描述】:

我需要将一个参数从一个 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);
        }
            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.");
                }
            });


    }

在logcat中显示如下

【问题讨论】:

  • logcat 中显示的那个应用不断停止消息是什么?
  • 你能发布关于崩溃的 logcat
  • 您好,感谢您的回复,我已经在logcat中添加了错误:)
  • 我认为错误截图没有帮助,似乎它没有指向实际错误
  • 对不起,我已经上传了另一张照片..

标签: java android parameter-passing


【解决方案1】:

注意数据类型:

在 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");
        }

然后再试一次

【讨论】:

  • 非常感谢,它有帮助:)
【解决方案2】:

试试这样example

<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();

    }

});

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

【讨论】:

    猜你喜欢
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    相关资源
    最近更新 更多