Android:为什么RadioButton返回NULL值?

时间:2015-08-10 22:02:55

标签: android sqlite radio-button android-radiogroup

我创建了一个RadioGroup来启用性别选择。

这是声明RadioGroup和RadioButtons的XML文件的一部分。

        <RadioGroup
            android:id="@+id/rgGenderGroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/rbMale"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:text="@string/m"
                android:textColor="@color/default_text"/>

            <RadioButton
                android:id="@+id/rbFemale"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:text="@string/f"
                android:textColor="@color/default_text"/>

        </RadioGroup>

现在,在活动中,我声明了RadioGroup和RadioButton变量并覆盖了onCheckedChanged方法。为了返回每个选择的某个值,我还声明了一个String值。

private String gender = "";
private RadioGroup rgGenderGroup;
private RadioButton rbMale, rbFemale;

...

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch(checkedId) {
            case(R.id.rbMale):
                gender = "M";
                break;
            case(R.id.rbFemale):
                gender = "F";
                break;
        }
    }

但是,当我尝试使用下面的查询将性别值插入数据库时​​,

private final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " ( " +
        COLUMN_NAME + " TEXT NOT NULL, " + COLUMN_AGE + " INT, " + COLUMN_NATIONALITY + " TEXT NOT NULL, " +
        COLUMN_GENDER + " CHAR(1) NOT NULL, " + COLUMN_CREATED_AT + " TEXT NOT NULL);";

失败,显示以下错误消息。

08-10 23:46:52.356  21666-21890/com.marshall.sortingtester E/SQLiteDatabase﹕ Error inserting Name=clara Age=0 Nationality=Angola Created_At=2015/08/10 23:46:52 Gender=null
    android.database.sqlite.SQLiteConstraintException: People.Gender may not be NULL (code 19)

为什么性别值即使在正确检查按钮后也会返回null?

编辑:这是insertData方法。

public void insertData(Person person) {
        ContentValues values = new ContentValues();
        values.put(SQLiteHandler.COLUMN_NAME, person.getName());
        values.put(SQLiteHandler.COLUMN_AGE, person.getAge());
        values.put(SQLiteHandler.COLUMN_NATIONALITY, person.getNationality());
        values.put(SQLiteHandler.COLUMN_GENDER, person.getGender());
        values.put(SQLiteHandler.COLUMN_CREATED_AT, person.getCreated_at());
        db.insert(SQLiteHandler.TABLE_NAME, null, values);
    }

1 个答案:

答案 0 :(得分:3)

RadioGroup rgGenderGroup= (RadioGroup) findViewById(R.id.rgGenderGroup);        
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // checkedId is the RadioButton selected
            switch(checkedId) {
            case(R.id.rbMale):
                person.setGender("M");
                break;
            case(R.id.rbFemale):
                 person.setGender("F");
                break;
        }
        }
    });