RadioButton setChecked NullPointerException

时间:2015-01-28 17:23:34

标签: java android radio-button radio-group

我不能让它工作,我不明白为什么。

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioButton.setChecked(boolean)' on a null object reference
        at eu.twenty1media.snakeplus.fragment.MainFragment.onCreateView(MainFragment.java:48)
    String lastlevel = msettings.getLevel();
    Log.d("MainFragment.java", "Last level from Database: radio"+lastlevel);

    RadioGroup rLevelGroup = (RadioGroup) getActivity().findViewById(R.id.radioLevel);
    int resourceId = this.getResources().getIdentifier("radio"+lastlevel, "id", getActivity().getPackageName());

    //rLevelGroup.check(R.id.radioextreme); //Nullpointer!

    //RadioButton levelbtn = (RadioButton) getActivity().findViewById(R.id.radioextreme); //NULLPOINTER
    //levelbtn.setChecked(true);

我在底部尝试了两种方法,但它们似乎都没有用,我总是得到一个NPE。我尝试使用resourceId String,但您可以看到正常的资源ID。

    <RadioGroup
    android:id="@+id/radioLevel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:orientation="horizontal"
    android:layout_marginBottom="16dp"
    android:gravity="center">
    <RadioButton
        android:layout_width="90dp"
        android:layout_height="wrap_content"
        android:id="@+id/radionormal"
        android:text="@string/level_normal"
        android:background="@drawable/radiolevel"
        android:button="@android:color/transparent"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:paddingTop="4dp"
        android:paddingBottom="4dp"
        android:layout_marginRight="2dp"
        android:layout_marginLeft="2dp"

        android:gravity="center"
        android:textColor="@color/orange_1"
        android:textStyle="bold"
        />
    <RadioButton
        android:layout_width="90dp"
        android:layout_height="wrap_content"
        android:id="@+id/radiohard"
        android:background="@drawable/radiolevel"
        android:button="@android:color/transparent"
        android:text="@string/level_hard"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:paddingTop="4dp"
        android:paddingBottom="4dp"
        android:layout_marginRight="2dp"
        android:layout_marginLeft="2dp"
        android:gravity="center"
        android:textColor="@color/orange_1"
        android:textStyle="bold"
        />
    <RadioButton
        android:layout_width="90dp"
        android:layout_height="wrap_content"
        android:id="@+id/radioextreme"
        android:background="@drawable/radiolevel"
        android:button="@android:color/transparent"
        android:text="@string/level_extreme"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:paddingTop="4dp"
        android:paddingBottom="4dp"
        android:layout_marginRight="2dp"
        android:layout_marginLeft="2dp"
        android:gravity="center"
        android:textColor="@color/orange_1"
        android:textStyle="bold"
        />
    </RadioGroup>

感谢您的帮助。

EDIT1:通过评论下面的行,我不会得到Nullpointer异常。

1 个答案:

答案 0 :(得分:1)

如果是活动:

RadioButton levelbtn = (RadioButton) findViewById(R.id.radioextreme); 
levelbtn.setChecked(true);

如果它在片段中。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.page_two, container, false);
    RadioButton levelbtn = (RadioButton) v.findViewById(R.id.radioextreme); 
    levelbtn.setChecked(true);
    return v;
}

您还可以在XML

中设置选中的单选按钮
<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:id="@+id/radioButton" />