RadioButton setChecked NullPointerException

RadioButton setChecked NullPointerException

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

 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);

我在底部尝试了两种方法,但其中 none 似乎有效,我总是得到 NPE。我尝试使用 resourceId 字符串,但您也可以看到正常的资源 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 异常。

如果是 activity:

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" />