RadioButton setText() 中的空指针异常“void android.widget.RadioButton.setText(java.lang.CharSequence)”
Posted
技术标签:
【中文标题】RadioButton setText() 中的空指针异常“void android.widget.RadioButton.setText(java.lang.CharSequence)”【英文标题】:Null Pointer Exception in RadioButton setText() "void android.widget.RadioButton.setText(java.lang.CharSequence)" 【发布时间】:2022-01-01 00:29:49 【问题描述】:“我收到此错误请帮助 - 在 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();
);
【问题讨论】:
这能回答你的问题吗? What is a NullPointerException, and how do I fix it? 【参考方案1】:在使用之前初始化 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());
【讨论】:
对不起,但它没有解决它显示相同的错误。 init rb_opted,rb_tossed 也带有 findViewById以上是关于RadioButton setText() 中的空指针异常“void android.widget.RadioButton.setText(java.lang.CharSequence)”的主要内容,如果未能解决你的问题,请参考以下文章
尝试在 recyclerview kotlin 上的空对象引用上调用虚拟方法 'void android.widget.TextView.setText(java.lang.CharSequence)