从片段中的选定RadioButton获取值

时间:2019-05-15 15:29:28

标签: java android

我在RadioButton中有两个RadioGroup。我还设置了一个带有setOnClickListener函数的Button。当按下按钮时,它应该根据选择的gender将浮点变量RadioButton设置为特定值。但是,只要按下该按钮,应用程序就会崩溃。

我尝试设置TextView只是为了查看它是否可以打印任何内容,但是按下按钮时应用程序崩溃了。我查看了Logcat,发现它引发了以下错误:java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.RadioButton.getText()' on a null object reference

package com.example.bacwatch;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class ProfileFragment extends Fragment {
    private EditText editWeight;
    private Button buttonConfirm;

    public float gender;
    RadioGroup radioGroup;
    RadioButton radioButton;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_profile, container, false);

        radioGroup = v.findViewById(R.id.gender_buttons);
        editWeight = v.findViewById(R.id.edit_Weight);
        buttonConfirm = v.findViewById(R.id.confirm_button);

        editWeight.addTextChangedListener(loginTextWatcher);

        // Button will input text.
        buttonConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Records the gender from the selected radio button.
                int radioID = radioGroup.getCheckedRadioButtonId();
                radioButton = v.findViewById(radioID);
                if (radioButton.getText().equals("Male")) {
                    gender = (float) .58;
                } else {
                    gender = (float) .49;
                }


                // Records User's weight (in pounds) and stores it into a variable.
                /*float weight = Float.valueOf(editWeight.getText().toString().trim()).floatValue();
                // Converts pounds to kilograms.
                float kilo_weight = (float) (weight/2.2046);
                // Calculates total body water.
                float body_water = kilo_weight*gender;*/

                // Clears text entries.
                editWeight.getText().clear();
            }
        });

        return v;
    }

    private TextWatcher loginTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String weightInput = editWeight.getText().toString().trim();

            buttonConfirm.setEnabled(!weightInput.isEmpty());
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };
}

XML文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@android:color/holo_green_light"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gender:"
        android:textSize="30sp"
        android:layout_centerHorizontal="true"
        android:id="@+id/gender_text"/>

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/gender_text"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal"
        android:id="@+id/gender_buttons">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Male"
                android:checked="true"
                android:textSize="30sp"
                android:id="@+id/male_button"/>

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Female"
                android:textSize="30sp"
                android:id="@+id/female_button"/>
    </RadioGroup>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/gender_buttons"
        android:id="@+id/weight_box">

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/weight_input">

            <android.support.design.widget.TextInputEditText
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:hint="Weight in Pounds"
                android:textSize="30sp"
                android:inputType="number"
                android:id="@+id/edit_Weight"/>
        </android.support.design.widget.TextInputLayout>
    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/weight_box"
        android:layout_centerHorizontal="true"
        android:textSize="30sp"
        android:text="Confirm"
        android:enabled="false"
        android:id="@+id/confirm_button"/>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

应用程序崩溃是因为您试图从处理点击的视图而非布局视图中获取radioButtion

radioButton = v.findViewById(radioID);

您必须使用另一个视图名称来设置它们之间的差异

buttonConfirm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Records the gender from the selected radio button.
            int radioID = radioGroup.getCheckedRadioButtonId();
            radioButton = v.findViewById(radioID);
            if (radioButton.getText().equals("Male")) {
                gender = (float) .58;
            } else {
                gender = (float) .49;
            }
         ......