尝试调用虚方法' void android.widget.RadioButton.setTypeface(android.graphics.Typeface)'在null对象引用上

时间:2017-07-30 06:12:04

标签: java android nullpointerexception

我正在实施广播组。当我尝试运行应用程序时,我收到此错误java.lang.NullPointerException:尝试调用虚方法' void android.widget.RadioGroup.setOnCheckedChangeListener(android.widget.RadioGroup $ OnCheckedChangeListener)'在null对象引用上。

radio_group_your_choice = (RadioGroup) findViewById(R.id.radio_group_your_choice);
        male_radioButton = (RadioButton) findViewById(R.id.male_radioButton);
        female_radioButton = (RadioButton) findViewById(R.id.female_radioButton);

        radio_group_your_choice.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId)
            {
                RadioButton radioButton = (RadioButton) findViewById(checkedId);
                gender = radioButton.getText().toString();
            }
        });

<RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="0dp"
                android:layout_marginBottom="15dp">

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

                    <RadioButton
                        android:id="@+id/delivery_radioButton"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Pick Up"
                        android:textColor="@color/dark_grey"/>

                    <RadioButton
                        android:id="@+id/pickup_radioButton"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="20dp"
                        android:layout_marginStart="20dp"
                        android:text="Delivery"
                        android:textColor="@color/dark_grey"/>


                </RadioGroup>

            </RelativeLayout>

1 个答案:

答案 0 :(得分:0)

此广播组为空:

radio_group_your_choice

尝试在setOnCheckedChangeListener

之前询问
if (radio_group_your_choice!= null)

避免这种做法。