Reputation: 135
I don't get this to work and I don't see why.
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);
I tried both methods at the bottom, but none of them seems to work, I always get a NPE. I tried using the resourceId String but as you can see a normal resource id aswell.
<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>
Thanks for the help.
EDIT1: With commenting the Lines below i don't get a Nullpointer Exception.
Upvotes: 0
Views: 4132
Reputation: 3568
If it is an an activity:
RadioButton levelbtn = (RadioButton) findViewById(R.id.radioextreme);
levelbtn.setChecked(true);
If it is in a 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;
}
You can also set a radio button checked within the XML
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:id="@+id/radioButton" />
Upvotes: 2