How to pass information from one java class to another

Kim E. R :

I need to pass one parameter from one Java class(A) to another Java class(B). I use many solutions from the Internet but it couldn't solve my problem. 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.

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. The application keep stopped when I click on the button in A class. But I think the problem is in B class. 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

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


    }

Below showed in the logcat error

LiuWenbin_NO. :

Take care of the data type:

In A class, you have:

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:

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.

So please change it to:

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

And try again

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=162598&siteId=1