Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 单选组按钮始终将默认值保存到sqlite中_Java_Android_Xml_Sqlite - Fatal编程技术网

Java 单选组按钮始终将默认值保存到sqlite中

Java 单选组按钮始终将默认值保存到sqlite中,java,android,xml,sqlite,Java,Android,Xml,Sqlite,我的问题是,每当我检查数据库去看病人时,我发现性别是男性,即使我检查了女性。 代码如下: 片段patient.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="mat

我的问题是,每当我检查数据库去看病人时,我发现性别是男性,即使我检查了女性。 代码如下: 片段patient.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="54dp"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/name_text"
            android:layout_width="143dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:ems="10"
            android:hint="@string/name_text_hint" />

        <EditText
            android:id="@+id/familyname_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:hint="@string/familyname_text_hint" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/date_text"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:ems="10"
            android:hint="@string/date_text_hint" >
        </EditText>
    </LinearLayout>

        <RadioGroup
        android:id="@+id/myRadioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:checkedButton="@+id/male_text" >


        <RadioButton
            android:id="@+id/female_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/female_text" />

        <RadioButton
            android:id="@+id/male_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/male_text" />
    </RadioGroup>


    <EditText
        android:id="@+id/address_text"
        android:layout_width="348dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/address_text_hint" />

    <LinearLayout
        android:layout_width="340dp"
        android:layout_height="57dp"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/phone_text"
            android:layout_width="143dp"
            android:layout_height="wrap_content"
            android:hint="@string/phone_text_hint" />

        <EditText
            android:id="@+id/email_text"
            android:layout_width="213dp"
            android:layout_height="wrap_content"
            android:hint="@string/email_text_hint" />
    </LinearLayout>

    <Button
        android:id="@+id/save_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="@string/save_button" />

</LinearLayout>

请帮助解决我的问题

您需要将check change listener添加到您的广播组中

radioG.setOnCheckedChangeListener(new     RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            int selectedId = radioG.getCheckedRadioButtonId();
            if(selectedId != -1) {
                selectedRadioButton = (RadioButton)v.findViewById(selectedId);

            }


        }
    });

您需要将检查更改侦听器添加到您的广播组

radioG.setOnCheckedChangeListener(new     RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            int selectedId = radioG.getCheckedRadioButtonId();
            if(selectedId != -1) {
                selectedRadioButton = (RadioButton)v.findViewById(selectedId);

            }


        }
    });

解决方案:1

onCreateView()
视图
对象声明为全局对象

View view;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
view = inflater.inflate(R.layout.fragment_patient, parent, false);
    ...
}
您的
radio.getCheckedRadioButtonId()
代码应该在
onClick()中

解决方案:2

  • 创建两个
    单选按钮的对象
  • 将其与
    onClick()


  • 解决方案:1

    onCreateView()
    视图
    对象声明为全局对象

    View view;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
    view = inflater.inflate(R.layout.fragment_patient, parent, false);
        ...
    }
    
    您的
    radio.getCheckedRadioButtonId()
    代码应该在
    onClick()中

    解决方案:2

  • 创建两个
    单选按钮的对象
  • 将其与
    onClick()


  • 你在哪里看到性别?在使用Firefox的sqlite管理器中,你在哪里看到性别?在使用firefoxthx的sqlite管理器中,但当我添加此代码时,v带下划线,它告诉我:不能在以不同方法定义的内部类中引用非最终变量v声明v为最终变量。最终视图v=充气器。充气(R.layout.fragment_patient,parent,false);不幸的是,应用程序崩溃了,给我这个:01-02 04:47:19.986:E/AndroidRuntime(1218):进程:com.example.appointapp,PID:1218 01-02 04:47:19.986:E/AndroidRuntime(1218):java.lang.NullPointerException 01-02 04:47:19.986:E/AndroidRuntime(1218):at com.example.appointapp.PatientFragment$2.onClick(PatientFragment.java:88)thx,但当我添加此代码时,v带下划线,它告诉我:不能在不同方法中定义的内部类中引用非最终变量v。将v声明为最终变量。最终视图v=充气器。充气(R.layout.fragment_patient,parent,false);不幸的是,应用程序崩溃了,给我这个:01-02 04:47:19.986:E/AndroidRuntime(1218):进程:com.example.appointapp,PID:1218 01-02 04:47:19.986:E/AndroidRuntime(1218):java.lang.NullPointerException 01-02 04:47:19.986:E/AndroidRuntime(1218):在com.example.appointapp.PatientFragment$2.onClick(PatientFragment.java:88)但是您应该检查它在哪里崩溃以及为什么崩溃?这是第一个解决方案的logcat输出:01-02 06:04:06.626:E/AndroidRuntime(1599):致命异常:main 01-02 06:04:06.626:E/AndroidRuntime(1599):Process:com.example.appointapp,PID:1599 01-02 06:04:06.626:E/AndroidRuntime(1599):java.lang.NullPointerException 01-02 06:04:06.626:E/AndroidRuntime(1599):在com.example.appointapp.PatientFragment$1.onClick(PatientFragment.java:83)01-02 06:04:06.626:E/AndroidRuntime(1599):在android.view.view.performClick(view.java:4438)但是您应该检查它在哪里崩溃以及为什么崩溃?这是第一个解决方案的logcat输出:01-02 06:04:06.626:E/AndroidRuntime(1599):致命异常:main 01-02 06:04:06.626:E/AndroidRuntime(1599):Process:com.example.appointapp,PID:1599 01-02 06:04:06.626:E/AndroidRuntime(1599):java.lang.NullPointerException 01-02 06:04:06.626:E/AndroidRuntime(1599):在com.example.appointapp.PatientFragment$1.onClick(PatientFragment.java:83)01-02 06:04:06.626:E/AndroidRuntime(1599):在android.view.view.performClick(view.java:4438)
    @Override
    public void onClick(View v){
    
      int selectedId = radioG.getCheckedRadioButtonId();
      if(selectedId == radio1.getId()) {    
          selectedRadioButton = radio1;
      }
      else if(selectedId == radio2.getId()) {    
          selectedRadioButton = radio2;
      }