android片段中的单选按钮验证

android片段中的单选按钮验证,android,Android,在我的程序中,我正在创建一个由名字、姓氏、性别、出生日期、地址1、地址2和手机号组成的用户配置文件片段。尽管我在单选按钮中进行了选择,但我在性别字段中得到了空值。并且我得到了“请填写性别字段”的警告。 我是android编程新手。帮我解决这个问题。 这是我的密码 package com.example.aparna.listfragment.fragments; import android.app.DatePickerDialog; import android.app.Dialog; im

在我的程序中,我正在创建一个由名字、姓氏、性别、出生日期、地址1、地址2和手机号组成的用户配置文件片段。尽管我在单选按钮中进行了选择,但我在性别字段中得到了空值。并且我得到了“请填写性别字段”的警告。 我是android编程新手。帮我解决这个问题。 这是我的密码

package com.example.aparna.listfragment.fragments;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;


import com.example.aparna.listfragment.R;
import com.example.aparna.listfragment.database.DbCreate;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class ProfileFragment extends Fragment {


    public TextView tv_firstname,tv_lastname,tv_dob,tv_setdate,tv_address1,tv_address2,tv_mob;
    public EditText edittext_firstname,edittext_lastname,edittext_address1,edittext_address2,edittext_mob;
    public ImageView img_same,img_calender;
    public Button btn_save;
    public RadioGroup radioGroup;
    public RadioButton radiobtn_male,radiobtn_female,rb;
    public DbCreate create;
    public String firstname,lastname,saddress1,saddress2,mobile_no,gender,date_of_birth;
    public int mYear,mMonth,mDay;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_profile, container, false);
        AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
        tv_firstname=(TextView)view.findViewById(R.id.tv_firstname);
        edittext_firstname=(EditText) view.findViewById(R.id.edittext_firstname);
        tv_lastname=(TextView)view.findViewById(R.id.tv_lastname);
        edittext_lastname=(EditText) view.findViewById(R.id.edittext_lastname);

        tv_address1=(TextView)view.findViewById(R.id.tv_address1);
        edittext_address1=(EditText)view.findViewById(R.id.edittext_address1);
        tv_address2=(TextView)view.findViewById(R.id.tv_address2);
        edittext_address2=(EditText)view.findViewById(R.id.edittext_address2);

        radioGroup=(RadioGroup)view.findViewById(R.id.radioGroup);
        btn_save=(Button)view.findViewById(R.id.btn_save);


        tv_dob=(TextView)view.findViewById(R.id.tv_dob);
        tv_setdate=(TextView) view.findViewById(R.id.tv_setdate);

        img_same=view.findViewById(R.id.img_same);
        img_calender=view.findViewById(R.id.img_calender);

        tv_dob=(TextView)view.findViewById(R.id.tv_mob);
        edittext_mob=(EditText) view.findViewById(R.id.edittext_mob);



        // date picker
        img_calender.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                final Calendar mcurrentDate=Calendar.getInstance();
                mYear=mcurrentDate.get(Calendar.YEAR);
                mMonth=mcurrentDate.get(Calendar.MONTH);
                mDay=mcurrentDate.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog mDatePicker=new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
                    public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
                        tv_setdate.setText(selectedday+"/"+selectedmonth+"/"+selectedyear);
                        date_of_birth=tv_setdate.getText().toString();
                        }
                },mYear,mMonth,mDay);
                mDatePicker.show();  }
        });



        //if address is same
        img_same.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edittext_address2.setText(edittext_address1.getText().toString());
                saddress2=saddress1;
                edittext_address2.setSelection(edittext_address2.getText().length());//moving cursor to the end of edit text

            }
        });






        // save button pressed
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                create=new DbCreate(getActivity());

                firstname=edittext_firstname.getText().toString();
                lastname=edittext_lastname.getText().toString();


                //radio button
                radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
                {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                       RadioButton rb= (RadioButton)group.findViewById(checkedId);
                        gender=rb.getText().toString();
                        Log.e("gender","=="+gender );
                    }
                });





                saddress1=edittext_address1.getText().toString();
                saddress2=edittext_address2.getText().toString();


                 mobile_no=edittext_mob.getText().toString();
                Log.e("mobile no ","=="+mobile_no);


//----------------------------------------------------------------------------------------------------

                if(firstname.isEmpty()) {
                    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                    alert.setTitle("ALERT");
                    alert.setIcon(R.drawable.ic_warning_black_24dp);
                    alert.setMessage("Please fill the firstname field");
                    alert.setPositiveButton("ok",null);
                    alert.show();
                }

               else if(!firstname.matches("[a-zA-Z ]+") ) {
                    edittext_firstname.setError("ENTER ONLY ALPHABETICAL CHARACTER");
                    }


                    else if(lastname.isEmpty()){
                    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                    alert.setTitle("ALERT");
                    alert.setIcon(R.drawable.ic_warning_black_24dp);
                    alert.setMessage("Please fill the lastname field");
                    alert.setPositiveButton("ok",null);
                    alert.show();
                  }

                else if( !lastname.matches("[a-zA-Z]+")) {
                    edittext_lastname.setError("ENTER ONLY ALPHABETICAL CHARACTER");
                }


                else if(gender.isEmpty()){
                    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                    alert.setTitle("ALERT");
                    alert.setIcon(R.drawable.ic_warning_black_24dp);
                    alert.setMessage("Please fill the gender field");
                    alert.setPositiveButton("ok",null);
                    alert.show();
                }

                else if(date_of_birth.isEmpty() ){
                    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                    alert.setTitle("ALERT");
                    alert.setIcon(R.drawable.ic_warning_black_24dp);
                    alert.setMessage("Please fill the dob field");
                    alert.setPositiveButton("ok",null);
                    alert.show();
                }

                else if( saddress1.isEmpty()){
                    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                    alert.setTitle("ALERT");
                    alert.setIcon(R.drawable.ic_warning_black_24dp);
                    alert.setMessage("Please fill the address1 field");
                    alert.setPositiveButton("ok",null);
                    alert.show();
                }
                else if(saddress2.isEmpty()){
                    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                    alert.setTitle("ALERT");
                    alert.setIcon(R.drawable.ic_warning_black_24dp);
                    alert.setMessage("Please fill the address2 field");
                    alert.setPositiveButton("ok",null);
                    alert.show();
                }

                else if(mobile_no.isEmpty()){
                    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                    alert.setTitle("ALERT");
                    alert.setIcon(R.drawable.ic_warning_black_24dp);
                    alert.setMessage("Please fill the mobile no field");
                    alert.setPositiveButton("ok",null);
                    alert.show();
                }
                else if( mobile_no.length()!=10){
                    edittext_mob.setError("ENTER VALID NUMBER");

                }

                else{
                    create.insertUser(firstname,lastname,gender,date_of_birth,saddress1,saddress2,mobile_no);
                    //create.deleteAll();
                    Toast.makeText(getActivity(), "data saved", Toast.LENGTH_SHORT).show();
                    }

                Log.e("no. of","data=="+create.userCount() );
                }
        });

        return view;
    }

}
请换这个

   RadioButton rb= (RadioButton)group.findViewById(checkedId);
为此:

将此粘贴到当前代码中的OnCreatedView从OnClick删除

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
            {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                   RadioButton rb= (RadioButton)view.findViewById(checkedId);
                    gender=rb.getText().toString();
                    Log.e("gender","=="+gender );
                }
            });
如果你想在按钮上点击这个按钮

是否在OnCreatedView中查找视图

 radioSexGroup=(RadioGroup)findViewById(R.id.radioGroup);
在单击时使用此按钮:

        int selectedId=radioSexGroup.getCheckedRadioButtonId();
        radioSexButton=(RadioButton)findViewById(selectedId);
片段:

  RadioButton rb= (RadioButton)view.findViewById(checkedId);

将您的checkedchangedlistener移到按钮之外,最好在onCreate()中单击。您好,请参阅本页,询问通常会被投票表决的好问题,这有助于获得好答案。当我将代码放入OnCreatedView方法中时,我在这一行获得了eeror。RadioButton rb=(RadioButton)findViewById(checkedId);哪一行???RadioButton rb=(RadioButton)findViewById(checkedId);由于无法解析findViewById(int)请参阅我编辑的答案片段,我收到错误