Group中的RadioButton给出了NullException

时间:2017-04-13 19:53:22

标签: java android sqlite radio-button

我正在尝试将用户的数据读入数据库,但没有存储单选按钮输入。它给出了一个null异常。面对同样的问题,将文本写入其他未被存储的活动中的文件。

以下是存储在数据库活动中的代码:

public class S4CharacterForm extends AppCompatActivity {

TextView t1, t2, t3, t4, t5, t6, t7, t8, t9;
EditText e1, e3, e4, e5, e6, e7, e8, e9;
RadioGroup rg;
RadioButton r2;
int selected_ID;
String radioText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_s4);

    ImageView imageView = (ImageView) findViewById(R.id.img_s3);
    Glide.with(this).load(R.drawable.banner_s3).into(imageView);

    Typeface tf = Typeface.createFromAsset(getAssets(), "font.ttf");
    t1 = (TextView) findViewById(R.id.name);
    t1.setTypeface(tf);
    t2 = (TextView) findViewById(R.id.gender);
    t2.setTypeface(tf);
    t3 = (TextView) findViewById(R.id.age);
    t3.setTypeface(tf);
    t4 = (TextView) findViewById(R.id.hair_c);
    t4.setTypeface(tf);
    t5 = (TextView) findViewById(R.id.eye_c);
    t5.setTypeface(tf);
    t6 = (TextView) findViewById(R.id.prof);
    t6.setTypeface(tf);
    t7 = (TextView) findViewById(R.id.dreams);
    t7.setTypeface(tf);
    t8 = (TextView) findViewById(R.id.peeves);
    t8.setTypeface(tf);
    t9 = (TextView) findViewById(R.id.purpose);
    t9.setTypeface(tf);

    e1 = (EditText) findViewById(R.id.name_ip);
    e3 = (EditText) findViewById(R.id.age_ip);
    e4 = (EditText) findViewById(R.id.hair_c_ip);
    e5 = (EditText) findViewById(R.id.eye_c_ip);
    e6 = (EditText) findViewById(R.id.prof_ip);
    e7 = (EditText) findViewById(R.id.dreams_ip);
    e8 = (EditText) findViewById(R.id.peeves_ip);
    e9 = (EditText) findViewById(R.id.purpose_ip);

    rg = (RadioGroup) findViewById(R.id.gender_ip);
    selected_ID = rg.getCheckedRadioButtonId();
    try {
        switch (selected_ID){
            case R.id.f:
                radioText = "Female";
                break;
            case R.id.m:
                radioText = "Male";
                break;
        }
        // radioText = (String) r2.getText();
    } catch (Exception e) {
        e.printStackTrace();
        Log.i(TAG, "Radio Button Null Exception");
        // r2 = (RadioButton) findViewById(selected_ID);
    }
}


public void nextScreen(View view) {

    CharacterTable charTable = new CharacterTable(this);
    charTable.open();

    long rowID = charTable.createCharacter(e1.getText().toString(), radioText, e3.getText().toString(),
            e4.getText().toString(), e5.getText().toString(), e6.getText().toString(), e7.getText().toString(),
            e8.getText().toString(), e9.getText().toString());

    if (rowID != -1)
        Toast.makeText(S4CharacterForm.this, "Character Created", Toast.LENGTH_SHORT).show();
    else
        Toast.makeText(S4CharacterForm.this, "Something went wrong", Toast.LENGTH_SHORT).show();
    charTable.close();
    Intent intent = new Intent(S4CharacterForm.this, S5.class);
    startActivity(intent);
}
}

我认为这是一个范围问题?任何人都可以对此有所了解吗?

1 个答案:

答案 0 :(得分:0)

尝试setOnCheckedChangeListener()

使用RadioGroup
rg  = (RadioGroup) findViewById(R.id.gender_ip);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) 
    {
         int id = rg.getCheckedRadioButtonId();
         switch (id) {
             case R.id.f:
                radioText = "Female";
                break;
            case R.id.m:
                radioText = "Male";
                break;
            default:
                // Your code
                break;
        }
    }
});