RadioButton setText() 中的空指针异常 "void android.widget.RadioButton.setText(java.lang.CharSequence)"

Null Pointer Exception in RadioButton setText() "void android.widget.RadioButton.setText(java.lang.CharSequence)"

“我收到此错误请帮助 - 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958) 由以下原因引起:java.lang.NullPointerException:尝试在空对象引用

上调用虚拟方法'void android.widget.RadioButton.setText(java.lang.CharSequence)'

包 com.codewithdevesh.cricketapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.google.android.material.textfield.TextInputLayout;

import java.util.HashMap;

public class TeamActivity extends AppCompatActivity {
    private TextInputLayout team1,team2,overs;
    private RadioGroup radioGroup1,radioGroup2;
    private RadioButton rb_toss_t1,rb_toss_t2,rb_opted,rb_tossed;
    private Button begin;
    String toss_team,opted_team;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_team);
        team1 = findViewById(R.id.tv_team1);
        team2 = findViewById(R.id.tv_team2);

        rb_toss_t1.setText(team1.getEditText().getText().toString());
        rb_toss_t2.setText(team2.getEditText().getText().toString());

        radioGroup1 = findViewById(R.id.radio_toss);
        radioGroup2 = findViewById(R.id.radio_opted);

        rb_toss_t1 = findViewById(R.id.rb_toss_t1);
        rb_toss_t2 = findViewById(R.id.rb_toss_t2);

        overs= findViewById(R.id.tv_overs);

        int id_toss = radioGroup1.getCheckedRadioButtonId();
        int id_opted = radioGroup2.getCheckedRadioButtonId();

        rb_tossed =radioGroup1.findViewById(id_toss);
        rb_opted = radioGroup2.findViewById(id_opted);

        toss_team = rb_tossed.getText().toString();
        opted_team = rb_opted.getText().toString();

        begin = findViewById(R.id.bttn_begin);



        begin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(TeamActivity.this,SelectPlayersActivity.class);
                i.putExtra("opted_t",opted_team);
                i.putExtra("toss_t",toss_team);
                i.putExtra("team1",team1.getEditText().getText().toString());
                i.putExtra("team2",team2.getEditText().getText().toString());
                startActivity(i);
                finish();
            }
        });
    }
}

在使用前初始化 rb_toss_t1 和 rb_toss_t2 并使用 findViewById 初始化 rb_opted,rb_tossed

   //no change
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_team);
    team1 = findViewById(R.id.tv_team1);
    team2 = findViewById(R.id.tv_team2);
    radioGroup1 = findViewById(R.id.radio_toss);
    radioGroup2 = findViewById(R.id.radio_opted);
    // findViewById first  before using them 
    rb_toss_t1 = findViewById(R.id.rb_toss_t1);
    rb_toss_t2 = findViewById(R.id.rb_toss_t2);
    //rb_opted,rb_tossed  init
    rb_opted = findViewById(R.id.rb_opted);
    rb_tossed  = findViewById(R.id.rb_tossed  );
    //then use function setText
    rb_toss_t1.setText(team1.getEditText().getText().toString());
    rb_toss_t2.setText(team2.getEditText().getText().toString());