gpt4 book ai didi

java - 单选按钮 setChecked NullPointerException

转载 作者:行者123 更新时间:2023-12-01 11:58:13 24 4
gpt4 key购买 nike

我无法让它工作,我也不明白为什么。

 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字符串,但正如你所看到的,也有一个普通的资源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>

感谢您的帮助。

编辑1:通过注释下面的行,我没有得到空指针异常。

最佳答案

如果它是一项 Activity :

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

如果它在 fragment 中。

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

关于java - 单选按钮 setChecked NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28198505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com