getCheckedRadioButtonId just gives me nullpoint exception

815 views Asked by At

Im having some issues, i have two Groups of Radiobuttons in my XML which has handling in an activity. It just crashes with nullpoint exception on line 48: int languangeId = languageGroup.getCheckedRadioButtonId();

I have set a "default" checked button in the XML to one of the buttons in the buttongroup, So why doesnt it get an valid value?:(

public class FirstTimeSelectMenu extends Activity{

private RadioGroup languageGroup;
private RadioGroup storageGroup;
private Button okButton;
private String getStorage;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.first_time_run);

    languageGroup = (RadioGroup)this.findViewById(R.id.languageGroup);
    storageGroup = (RadioGroup)this.findViewById(R.id.storageGroup);
    okButton = (Button)findViewById(R.id.okBtn);



    okButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {




            int languangeId = languageGroup.getCheckedRadioButtonId();
            int storageId = storageGroup.getCheckedRadioButtonId();
            RadioButton languageb = (RadioButton)findViewById(languangeId);
            RadioButton storageb = (RadioButton)findViewById(storageId);

            if(languageb.equals("Norwegian")){
                    //Need to fix this!
            }
            if(languageb.equals("English")){
                    //Need to fix this!
            }
            if(storageb.equals("SD Card")){
               String sdStoragePath = Environment.getExternalStorageDirectory().toString() + "/PictureTalk/";
               FileInteraction fi = new FileInteraction();
               fi.firstTimeFillPath(getResources(), "PictureTalk/Food", sdStoragePath +"PictureTalk/Food");
               fi.firstTimeFillPath(getResources(), "PrivatePictures", Environment.getExternalStorageDirectory().toString() +"PrivatePictures");
               Intent intent = new Intent(getApplicationContext(),MainMenuActivity.class);
               intent.putExtra("dataStorePath",sdStoragePath);
               startActivity(intent);
            }
        }
    });
}}

My XML file

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView
    android:id="@+id/languagetext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="236dp"
    android:text="Choose language" />

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignEnd="@+id/languagetext"
    android:layout_alignRight="@+id/languagetext"
    android:layout_below="@+id/languageGroup">



    <RadioButton
        android:id="@+id/norwegianRadioBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="Norwegian" />

    <RadioButton
        android:id="@+id/englishRadioBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="English" />

</RadioGroup>


<TextView
    android:id="@+id/internal_sd"
    android:layout_width="464dp"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="236dp"
    android:text="Do you want to store the data to:" />

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:id="@+id/storageGroup">



    <RadioButton
        android:id="@+id/sdcardRadioBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:checked="true"
        android:text="SD card" />


    <RadioButton
        android:id="@+id/internalRadioBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="Internal storage" />
</RadioGroup>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Ok"
    android:id="@+id/okBtn"
    android:layout_gravity="bottom" />

1

There are 1 answers

3
Dillon Drobena On BEST ANSWER

To start off with, what element is assigned the id "@+id/languageGroup" since it doesn't appear to be declared in your XML